我无法从 Sqlite 数据库中获取数据
I cannot get the data from the Sqlite database
我无法从 Sqlite 数据库中获取数据。
它不会为我打印任何东西。我检查了与数据库的连接是否已建立并且数据库包含数据。
它没有给出错误但不打印任何内容。
当我启动应用程序时,tableView 不包含任何记录。
在控制台中打印:
API 使用 NULL 数据库连接指针调用
在 [d24547a13b]
的第 139466 行滥用
准备:内存不足
// AppDelegate.swift
import UIKit
import SQLite3
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DataManager.dataManager.dbOpaquePointer = DataManager.dataManager.openDatabase(dbName: "DB.sqlite")
return true
}
}
// TableWord.swift
import Foundation
class TableWord{
var idword: Int = 0
var word: String = ""
var features: String = ""
var number: Int = 0
init(idword: Int, word: String, features: String, number: Int) {
self.idword = idword
self.word = word
self.features = features
self.number = number
}
}
// DataManager.swift
import UIKit
import SQLite3
class DataManager {
static let dataManager = DataManager()
var dbOpaquePointer: OpaquePointer?
// OPEN
func openDatabase(dbName: String) -> OpaquePointer? {
let documentDirectory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = documentDirectory?.appendingPathComponent(dbName).relativePath
guard let part1DbPath = fileUrl else {
print("part1DbPath is nil.")
return nil
}
if sqlite3_open(part1DbPath, &dbOpaquePointer) == SQLITE_OK {
print(“Open database in \(part1DbPath)")
return dbOpaquePointer
} else {
print("\(Errors.SQLiteError.openDatabase)")
}
return dbOpaquePointer
}
// SELECT
func selectTableWord() -> [TableWord] {
let querySelectTableWord = "SELECT * FROM tableword;"
var dbOpaquePointer2: OpaquePointer? = nil
var dataTableWord: [TableWord] = []
if sqlite3_prepare_v2(dbOpaquePointer, querySelectTableWord, -1, &dbOpaquePointer2, nil) == SQLITE_OK {
while (sqlite3_step(dbOpaquePointer2) == SQLITE_ROW) {
let idword = sqlite3_column_int(dbOpaquePointer2, 0)
guard let queryResultCol1 = sqlite3_column_text(dbOpaquePointer2, 1) else {
print("\(Errors.SQLiteError.queryNil)")
return dataTableWord
}
let word = String(cString: queryResultCol1)
guard let queryResultCol2 = sqlite3_column_text(dbOpaquePointer2, 2) else {
print("\(Errors.SQLiteError.queryNil)")
return dataTableWord
}
let features = String(cString: queryResultCol2)
let number = sqlite3_column_int(dbOpaquePointer2, 3)
dataTableWord.append(TableWord(idword: Int(idword), word: word, features: features, number: Int(number)))
}
} else {
let errorMessage = String(cString: sqlite3_errmsg(dbOpaquePointer2))
print("\(Errors.SQLiteError.prepare): \(errorMessage)")
}
sqlite3_finalize(dbOpaquePointer2)
return dataTableWord
}
}
// ViewController.swift
import UIKit
import SQLite3
class ViewController: UIViewController, UITextViewDelegate, UITableViewDataSource {
@IBOutlet weak var resultTableView: UITableView!
var db: DataManager = DataManager()
var dataTableWord: [TableWord] = []
override func viewDidLoad() {
super.viewDidLoad()
dataTableWord = db.selectTableWord()
searchTextView.delegate = self
resultTableView.dataSource = self
}
//MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataTableWord.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idCell1Main1", for: indexPath) as! TableViewCell
cell.cellLabelNumber.text = String(dataTableWord[indexPath.row].idword)
cell.cellLabelTitle.text = dataTableWord[indexPath.row].word
cell.cellLabelSubtitle.text = dataTableWord[indexPath.row].features
return cell
}
}
在你调用的viewDidLoad中
var db: DataManager = DataManager()
这意味着您正在创建 DataManager 的新实例,而不是使用您在应用程序委托中创建的具有打开的数据库连接的实例
您应该始终使用您创建的静态属性访问您的数据库
let db = DataManager.databaseManager
为避免此类错误,您可以将私有初始化添加到 class
private init() {}
这样你只能通过DataManager.databaseManager
访问它
我无法从 Sqlite 数据库中获取数据。 它不会为我打印任何东西。我检查了与数据库的连接是否已建立并且数据库包含数据。 它没有给出错误但不打印任何内容。 当我启动应用程序时,tableView 不包含任何记录。
在控制台中打印:
API 使用 NULL 数据库连接指针调用
在 [d24547a13b]
的第 139466 行滥用
准备:内存不足
// AppDelegate.swift
import UIKit
import SQLite3
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DataManager.dataManager.dbOpaquePointer = DataManager.dataManager.openDatabase(dbName: "DB.sqlite")
return true
}
}
// TableWord.swift
import Foundation
class TableWord{
var idword: Int = 0
var word: String = ""
var features: String = ""
var number: Int = 0
init(idword: Int, word: String, features: String, number: Int) {
self.idword = idword
self.word = word
self.features = features
self.number = number
}
}
// DataManager.swift
import UIKit
import SQLite3
class DataManager {
static let dataManager = DataManager()
var dbOpaquePointer: OpaquePointer?
// OPEN
func openDatabase(dbName: String) -> OpaquePointer? {
let documentDirectory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = documentDirectory?.appendingPathComponent(dbName).relativePath
guard let part1DbPath = fileUrl else {
print("part1DbPath is nil.")
return nil
}
if sqlite3_open(part1DbPath, &dbOpaquePointer) == SQLITE_OK {
print(“Open database in \(part1DbPath)")
return dbOpaquePointer
} else {
print("\(Errors.SQLiteError.openDatabase)")
}
return dbOpaquePointer
}
// SELECT
func selectTableWord() -> [TableWord] {
let querySelectTableWord = "SELECT * FROM tableword;"
var dbOpaquePointer2: OpaquePointer? = nil
var dataTableWord: [TableWord] = []
if sqlite3_prepare_v2(dbOpaquePointer, querySelectTableWord, -1, &dbOpaquePointer2, nil) == SQLITE_OK {
while (sqlite3_step(dbOpaquePointer2) == SQLITE_ROW) {
let idword = sqlite3_column_int(dbOpaquePointer2, 0)
guard let queryResultCol1 = sqlite3_column_text(dbOpaquePointer2, 1) else {
print("\(Errors.SQLiteError.queryNil)")
return dataTableWord
}
let word = String(cString: queryResultCol1)
guard let queryResultCol2 = sqlite3_column_text(dbOpaquePointer2, 2) else {
print("\(Errors.SQLiteError.queryNil)")
return dataTableWord
}
let features = String(cString: queryResultCol2)
let number = sqlite3_column_int(dbOpaquePointer2, 3)
dataTableWord.append(TableWord(idword: Int(idword), word: word, features: features, number: Int(number)))
}
} else {
let errorMessage = String(cString: sqlite3_errmsg(dbOpaquePointer2))
print("\(Errors.SQLiteError.prepare): \(errorMessage)")
}
sqlite3_finalize(dbOpaquePointer2)
return dataTableWord
}
}
// ViewController.swift
import UIKit
import SQLite3
class ViewController: UIViewController, UITextViewDelegate, UITableViewDataSource {
@IBOutlet weak var resultTableView: UITableView!
var db: DataManager = DataManager()
var dataTableWord: [TableWord] = []
override func viewDidLoad() {
super.viewDidLoad()
dataTableWord = db.selectTableWord()
searchTextView.delegate = self
resultTableView.dataSource = self
}
//MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataTableWord.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idCell1Main1", for: indexPath) as! TableViewCell
cell.cellLabelNumber.text = String(dataTableWord[indexPath.row].idword)
cell.cellLabelTitle.text = dataTableWord[indexPath.row].word
cell.cellLabelSubtitle.text = dataTableWord[indexPath.row].features
return cell
}
}
在你调用的viewDidLoad中
var db: DataManager = DataManager()
这意味着您正在创建 DataManager 的新实例,而不是使用您在应用程序委托中创建的具有打开的数据库连接的实例
您应该始终使用您创建的静态属性访问您的数据库
let db = DataManager.databaseManager
为避免此类错误,您可以将私有初始化添加到 class
private init() {}
这样你只能通过DataManager.databaseManager