我试图只为我的sqlite文件插入一个名称。即时通讯使用此代码,但它无法正常工作:/
-(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);
}
答案 0 :(得分:2)
将您的查询传递给此方法并尝试
-(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);
}
答案 1 :(得分:1)
"内存不足"来自sqlite的错误通常意味着您正在使用的数据库句柄尚未打开。确保使用您发布的代码中显示的sqlite3_open_v2
变量来调用database
答案 2 :(得分:1)
好吧我终于成功了!这是我用于需要它的每个人的代码:
-(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);
}
}
答案 3 :(得分:1)
试试这个:
//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;
}