Hello,
I am very new to programming, Swift, Xcode and SQLite. I have researched the possible answer to this issue but I feel I need some expert help to understand this further please.
I have an example Swift program that enters data into, out etc from SQLite database. I have not included all sections of this program but only the function where the error is. The entire function is listed at the bottom of this question.
The error that shows is "Value of type '[NSString]' has no member 'utf8String'" and only appears on the line "sqlite3bindtext(insertStatement, 3, address.utf8String, -1, nil)".
In the code at the bottom of this question, The line above the error line performs the same database input function for column 2 and is prefixed with "name.utf8String" but it does not have any error and when I remove the Error line the program runs and performs as expected.
This error is stating that it is a "Type" error which my research has uncovered relates to variables, arrays and ints etc where the data type is trying to be converted to another data type. However from the NSString type documents, it says that it can convert to utf8String along with other types, so I am not sure why this particular line shows an error and the one before does not and how to fix it.
Can you please help me to understand this further and how to fix it please?
func insertname() {
var insertStatement: OpaquePointer?
let name: [NSString] = ["Ray", "Chris", "Martha", "Danielle"]
let address: [NSString] = ["EducationRd", "HillRd", "ValleyDr", "SouthRd"]
// let str = NSString(decoding: address, as:UTF8.self)
if sqlite3preparev2(db,insertStatementString,-1,&insertStatement,nil) == SQLITEOK {
print("\n MULTIPLE NAME INSERTS CHALLENGE: NAMES IN ARRAY = \(name)")
print("\n MULTIPLE ADDRESS INSERTS CHALLENGE: ADDRESSES IN ARRAY = \(address)")
for (index, name) in name.enumerated() {
let id = Int32(index + 1)
sqlite3bindint(insertStatement, 1, id)
sqlite3bindtext(insertStatement, 2, name.utf8String, -1, nil)
sqlite3bindtext(insertStatement, 3, address.utf8String, -1, nil)
if sqlite3step(insertStatement) == SQLITEDONE {
print("SUCCESSFULLY INSERTED ROW ID = \(id), NAME = \(name), ADDRESS = \(address)")
} else {
print("COULD NOT INSERT ROW. ID = \(id), NAME = \(name), ADDRESS = \(address)")
}
sqlite3reset(insertStatement)
}
sqlite3_finalize(insertStatement)
} else {
print("SUCCESSFULLY INSERTED ROW.")
}
}
I am very new to programming, Swift, Xcode and SQLite. I have researched the possible answer to this issue but I feel I need some expert help to understand this further please.
I have an example Swift program that enters data into, out etc from SQLite database. I have not included all sections of this program but only the function where the error is. The entire function is listed at the bottom of this question.
The error that shows is "Value of type '[NSString]' has no member 'utf8String'" and only appears on the line "sqlite3bindtext(insertStatement, 3, address.utf8String, -1, nil)".
In the code at the bottom of this question, The line above the error line performs the same database input function for column 2 and is prefixed with "name.utf8String" but it does not have any error and when I remove the Error line the program runs and performs as expected.
This error is stating that it is a "Type" error which my research has uncovered relates to variables, arrays and ints etc where the data type is trying to be converted to another data type. However from the NSString type documents, it says that it can convert to utf8String along with other types, so I am not sure why this particular line shows an error and the one before does not and how to fix it.
Can you please help me to understand this further and how to fix it please?
func insertname() {
var insertStatement: OpaquePointer?
let name: [NSString] = ["Ray", "Chris", "Martha", "Danielle"]
let address: [NSString] = ["EducationRd", "HillRd", "ValleyDr", "SouthRd"]
// let str = NSString(decoding: address, as:UTF8.self)
if sqlite3preparev2(db,insertStatementString,-1,&insertStatement,nil) == SQLITEOK {
print("\n MULTIPLE NAME INSERTS CHALLENGE: NAMES IN ARRAY = \(name)")
print("\n MULTIPLE ADDRESS INSERTS CHALLENGE: ADDRESSES IN ARRAY = \(address)")
for (index, name) in name.enumerated() {
let id = Int32(index + 1)
sqlite3bindint(insertStatement, 1, id)
sqlite3bindtext(insertStatement, 2, name.utf8String, -1, nil)
sqlite3bindtext(insertStatement, 3, address.utf8String, -1, nil)
if sqlite3step(insertStatement) == SQLITEDONE {
print("SUCCESSFULLY INSERTED ROW ID = \(id), NAME = \(name), ADDRESS = \(address)")
} else {
print("COULD NOT INSERT ROW. ID = \(id), NAME = \(name), ADDRESS = \(address)")
}
sqlite3reset(insertStatement)
}
sqlite3_finalize(insertStatement)
} else {
print("SUCCESSFULLY INSERTED ROW.")
}
}