如何使用SQlite将数据写入数据库?

时间:2011-02-23 07:39:54

标签: iphone sql sqlite

我曾使用此代码将条目写入数据库,但它没有反映到它...我不知道为什么?请提出你的建议!!提前致谢..

NSString* user_Name=txt_UserName.text;
NSString* password=txt_Password.text;
NSString* rePassword=txt_RePassword.text;
if ([password isEqualToString:rePassword]) {

sqlite3 *database;
sqlite3_stmt *compiledQuery;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "insert into Main values(?,?)";
    sqlite3_prepare_v2(database, sqlStatement, -1, &compiledQuery, NULL);
    sqlite3_bind_text(compiledQuery, 1, [user_Name UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(compiledQuery, 2, [password UTF8String], -1, SQLITE_TRANSIENT);       
    if(SQLITE_DONE != sqlite3_step(compiledQuery))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    sqlite3_finalize(compiledQuery);
    sqlite3_close(database);
}

1 个答案:

答案 0 :(得分:0)

是数据库的路径是否正确?您可以通过

检查数据库路径
NSString *documentsDir = [DbCls getPath];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:DBNAME];

+(NSString *) getPath
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    return documentsDir;
}