SQLite iOS插入数据
我试图在我的sqlite文件中只插入一个名称。我正在使用此代码,但它不起作用:/SQLite iOS插入数据,ios,sqlite,insert,Ios,Sqlite,Insert,我试图在我的sqlite文件中只插入一个名称。我正在使用此代码,但它不起作用:/ -(void)InsertRecords:(NSMutableString *) txt{ if(addStmt == nil) { const char *sql = "INSERT INTO myMovies (movieName) VALUES(?) "; if(sqlite3_prepare_v2(database, sql, -1, &addStmt, N
-(void)InsertRecords:(NSMutableString *) txt{
if(addStmt == nil) {
const char *sql = "INSERT INTO myMovies (movieName) VALUES(?) ";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
else
sqlite3_bind_text(addStmt, 1, [txt UTF8String], -1, SQLITE_TRANSIENT);
}
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
sqlite3_reset(addStmt);
}
sqlite的内存不足错误通常意味着您正在使用的数据库句柄尚未打开。确保调用sqlite3\u open\u v2时使用的数据库变量显示在您发布的代码中?sqlite内存不足错误通常意味着您正在使用的数据库句柄尚未打开。确保您正在使用发布的代码中显示的数据库变量调用sqlite3\u open\u v2?将查询传递到此方法,然后重试
-(void)Insertdata:(NSString*)query{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"YourDBName.sql"];
if(sqlite3_open([databasePath UTF8String],&db) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"%@",query];
char *errmsg=nil;
if(sqlite3_exec(db, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(@".. Row Added ..");
}
}
sqlite3_close(db);
}
将查询传递到此方法,然后重试
-(void)Insertdata:(NSString*)query{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"YourDBName.sql"];
if(sqlite3_open([databasePath UTF8String],&db) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"%@",query];
char *errmsg=nil;
if(sqlite3_exec(db, [querySQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(@".. Row Added ..");
}
}
sqlite3_close(db);
}
好的,我终于成功了!这是我为每个需要它的人使用的代码:
-(void)InsertRecords:(NSMutableString *)txt{
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"movieData.sqlite"];
const char *dbpath = [dbPath UTF8String];
sqlite3 *contactDB;
sqlite3_stmt *statement;
NSLog(@"%@",dbPath);
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO myMovies (movieName) VALUES (\"%@\")", txt];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_bind_text(statement, 1, [txt UTF8String], -1, SQLITE_TRANSIENT);
} else {
NSLog(@"error");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
好的,我终于成功了!这是我为每个需要它的人使用的代码:
-(void)InsertRecords:(NSMutableString *)txt{
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"movieData.sqlite"];
const char *dbpath = [dbPath UTF8String];
sqlite3 *contactDB;
sqlite3_stmt *statement;
NSLog(@"%@",dbPath);
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO myMovies (movieName) VALUES (\"%@\")", txt];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_bind_text(statement, 1, [txt UTF8String], -1, SQLITE_TRANSIENT);
} else {
NSLog(@"error");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
试试这个:
//save our data
- (BOOL) saveEmployee:(Employee *)employee
{
BOOL success = false;
sqlite3_stmt *statement = NULL;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &mySqliteDB) == SQLITE_OK)
{
if (employee.employeeID > 0) {
NSLog(@"Exitsing data, Update Please");
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE EMPLOYEES set name = '%@', department = '%@', age = '%@' WHERE id = ?",
employee.name,
employee.department,
[NSString stringWithFormat:@"%d", employee.age]];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(mySqliteDB, update_stmt, -1, &statement, NULL );
sqlite3_bind_int(statement, 1, employee.employeeID);
if (sqlite3_step(statement) == SQLITE_DONE)
{
success = true;
}
}
else{
NSLog(@"New data, Insert Please");
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO EMPLOYEES (name, department, age) VALUES (\"%@\", \"%@\", \"%@\")",
employee.name,
employee.department,
[NSString stringWithFormat:@"%d", employee.age]];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(mySqliteDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
success = true;
}
}
sqlite3_finalize(statement);
sqlite3_close(mySqliteDB);
}
return success;
}
试试这个:
//save our data
- (BOOL) saveEmployee:(Employee *)employee
{
BOOL success = false;
sqlite3_stmt *statement = NULL;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &mySqliteDB) == SQLITE_OK)
{
if (employee.employeeID > 0) {
NSLog(@"Exitsing data, Update Please");
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE EMPLOYEES set name = '%@', department = '%@', age = '%@' WHERE id = ?",
employee.name,
employee.department,
[NSString stringWithFormat:@"%d", employee.age]];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(mySqliteDB, update_stmt, -1, &statement, NULL );
sqlite3_bind_int(statement, 1, employee.employeeID);
if (sqlite3_step(statement) == SQLITE_DONE)
{
success = true;
}
}
else{
NSLog(@"New data, Insert Please");
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO EMPLOYEES (name, department, age) VALUES (\"%@\", \"%@\", \"%@\")",
employee.name,
employee.department,
[NSString stringWithFormat:@"%d", employee.age]];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(mySqliteDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
success = true;
}
}
sqlite3_finalize(statement);
sqlite3_close(mySqliteDB);
}
return success;
}
@adum错误是:-[MyDvDsList InsertRecords:]、/Users/../MyDvDsList.m:52 2012-10-25 02:00:54.916 MyDVD[282887:14003]***由于未捕获的异常“NSInternalInconsistencyException”终止应用程序,原因是:“创建add语句时出错。”内存不足当您出现内存不足错误时,这实际上令人困惑地意味着数据库的sqlite3指针为空,这通常是无法正确打开数据库的症状。@adum错误是:-[MyDvDsList InsertRecords:],/Users/../MyDvDsList.m:52 2012-10-25 02:00:54.916 MyDVD[28287:14003]中的断言失败***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“创建add语句时出错。”内存不足当您出现内存不足错误时,这实际上意味着数据库的sqlite3指针为空,这通常是无法正确打开数据库的征兆。这是不正确的。首先,将数据插入捆绑包中的数据库,该捆绑包是只读的。改为使用文档文件夹。其次,您正在使用stringWithFormat构建SQL,但是如果该值包含引号,SQL将失败。总是用吗?占位符和sqlite3\u在SQL语句中用字符串值绑定\u XXX。第三,我通常建议将sqlite3_errmsg记录在else子句中,这样如果失败,您就会知道原因。第四,我建议不要每次都打开和关闭数据库。当应用程序启动时打开数据库,并在终止时关闭。这对SQL注入攻击是开放的。这是不正确的。首先,将数据插入捆绑包中的数据库,该捆绑包是只读的。改为使用文档文件夹。其次,您正在使用stringWithFormat构建SQL,但是如果该值包含引号,SQL将失败。总是用吗?占位符和sqlite3\u在SQL语句中用字符串值绑定\u XXX。第三,我通常建议将sqlite3_errmsg记录在else子句中,这样如果失败,您就会知道原因。第四,我建议不要每次都打开和关闭数据库。当应用程序启动时打开数据库,并在终止时关闭。这会受到SQL注入攻击