我有以下Database类创建数据库并包含将Movie添加到数据库的方法。我在那里有几个打印语句用于调试。
import Foundation
import SQLite3
class SQLiteDatabase
{
private var db : OpaquePointer?
init (databaseName dbName: String){
//Get a file handle, or create handle.
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("\(dbName).sqlite")
//try and open filepath as databse
if sqlite3_open(fileURL.path, &db) == SQLITE_OK
{
print("Sucessfully opened connection to database as \(fileURL.path)")
}
else
{
print("Unable to open databse at \(fileURL.path)")
printCurrentSQLErrorMessage(db)
}
}
deinit {
sqlite3_close(db)
}
func insert(movie:Movie){
let insertStatementQuery =
"INSERT INTO Movie (ID,Name,Year,Director) VALUES (?,?,?,?);"
var insertStatement: OpaquePointer?
if sqlite3_prepare_v2(db, insertStatementQuery, -1, &insertStatement, nil) == SQLITE_OK
{
sqlite3_bind_int(insertStatement, 1, movie.ID)
sqlite3_bind_text(insertStatement, 2, movie.name, -1, nil)
sqlite3_bind_int(insertStatement, 3, movie.year)
sqlite3_bind_text(insertStatement, 4, movie.director, -1, nil)
print("Name value in is: \(movie.name), Director Value in is: \(movie.director)")
if sqlite3_step(insertStatement) == SQLITE_DONE
{
print("Sucesfully inserted row")
}
else
{
print("Could not insert row")
printCurrentSQLErrorMessage(db)
}
}
else
{
print("INSERT statement could not be prepared.")
printCurrentSQLErrorMessage(db)
}
sqlite3_finalize(insertStatement)
}
func selectAllMovie() -> [Movie]{
var result = [Movie]()
let selectStatementQuery = "SELECT ID,Name,Year,Director FROM Movie"
//prepare statement
var selectStatement : OpaquePointer? = nil
if sqlite3_prepare_v2(db, selectStatementQuery, -1 , &selectStatement, nil) == SQLITE_OK
{
//iterate over result
while sqlite3_step(selectStatement) == SQLITE_ROW
{
//create movie object from result
let movie = Movie(
ID: sqlite3_column_int(selectStatement, 0),
name: String(cString:sqlite3_column_text(selectStatement, 1)),
year: sqlite3_column_int(selectStatement, 2),
director: String(cString:sqlite3_column_text(selectStatement, 3))
)
print(String(cString: sqlite3_column_text(selectStatement, 1)))
print(String(cString: sqlite3_column_text(selectStatement, 3)))
print("Name value out is: \(movie.name), Director Value out is: \(movie.director)")
//add result to array
result += [movie]
}
}
else
{
print("SELECT statement could not be prepared.")
printCurrentSQLErrorMessage(db)
}
//cleanup
sqlite3_finalize(selectStatement)
return result
}
func printCurrentSQLErrorMessage(_ db : OpaquePointer?){
let errorMessage = String.init(cString : sqlite3_errmsg(db))
print ("Error:\(errorMessage)")
}
func createMovieTable(){
let createTableQuery = """
CREATE TABLE Movie (
ID INTEGER PRIMARY KEY NOT NULL,
Name CHAR(255),
Year INTEGER,
Director CHAR(255)
);
"""
//prepare statement
var createTableStatement : OpaquePointer? = nil
if sqlite3_prepare_v2(db, createTableQuery, -1, &createTableStatement, nil) == SQLITE_OK
{
//execute the statement
if sqlite3_step(createTableStatement) == SQLITE_DONE
{
print("Movie Table Done")
}
else
{
print("Movie table could not be created")
printCurrentSQLErrorMessage(db)
}
}
else
{
print("CREATE TABLE statement could not be prepared")
printCurrentSQLErrorMessage(db)
}
//cleanup
sqlite3_finalize(createTableStatement)
}
}
我有以下代码添加两个条目
database.createMovieTable()
database.insert(movie: Movie(ID: 0, name: "ExtraLong", year: 2018, director: "Bob Ross"))
database.insert(movie: Movie(ID: 1, name: "ExtraExtraLong", year: 2018, director: "Donald Putin"))
我遇到的问题是Name变量的输出实际上与Director变量相同,我无法弄清楚原因。 名称值out的长度通常与名称值的长度相同,这意味着如果Director长于名称,则只有部分director值被分配给名称值,尽管不正确。
我的出局看起来像这样:
名称值为:ExtraLong,Director Value in:Bob Ross
成功插入行
名称 - >鲍勃罗斯
导演 - >鲍勃罗斯
名称价值是:鲍勃罗斯,董事价值出来是:鲍勃罗斯