我試圖在Swift中創建一個簡單的sqlite數據庫,但我在嘗試創建表時收到錯誤(特別是SQLITE_ERROR)。Swift Sqlite SQL錯誤
這裏是我的代碼:
var db :OpaquePointer?
let dbPath = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent("\(Date.init().timeIntervalSince1970)".replacingOccurrences(of: ".", with: "") + ".db")
.absoluteString
var returnCode :Int32 = sqlite3_open(dbPath.cString(using: .utf8), &db)
if SQLITE_OK != returnCode {
preconditionFailure("Failed to open db")
}
var stmt :OpaquePointer?
returnCode = sqlite3_prepare_v2(db, "CREATE TABLE Things (name TEXT)".cString(using: .utf8), -1, &stmt, nil)
if SQLITE_OK != returnCode {
preconditionFailure("Failed to prepare table creation SQL")
}
SQLite是通過Cocoapod包括在內。我試過在轉換爲C字符串時使用不同的字符串編碼,特別是我嘗試過使用ASCII編碼,而且我也嘗試了對數據庫名稱進行硬編碼。
該錯誤發生在sqlite3_prepare_v2
。
的錯誤消息是「近\」 \∪{01} \「:語法錯誤」
使用'sqlite3_errmsg()'打印錯誤原因。 - 請注意,您可以將Swift字符串直接傳遞給C函數,例如'sqlite3_open(dbPath,&db)' –
@MartinR錯誤信息是「near \」\ u {01} \「:語法錯誤」 –
http://stackoverflow.com/questions/34135305/nsfilemanager-defaultmanager-fileexistsatpath的副本-returns-false-instead-of-true? - 不要使用'absoluteString'來獲取文件路徑。 –