我制作了iPad应用程序,
我想将记录插入数据库表,但我无法做同样的事情。
这是我的代码段,
-(void) insertRecordIntoTableNamed: (NSString *) symbol{
NSString *sql = [NSString stringWithFormat:@"INSERT INTO recentquotes ('symbol', 'dt_tm') VALUES ('%@',datetime())",symbol];
NSLog(@"sql=%@",sql);
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0, @"Error updating table.");
}
}
我的NSLog显示:
sql=INSERT INTO recentquotes ('symbol', 'dt_tm') VALUES ('PATNI',datetime())
此声明是正确的,但我无法在数据库表中看到VALUES PATNI
和datetime()
这是代码的其余部分,
NSString *filePahs = Nil;
-(NSString *) filePath {
filePahs=[[NSBundle mainBundle] pathForResource:@"companymaster" ofType:@"sql"];
NSLog(@"path=%@",filePahs);
return filePahs;
}
上述方法的结果是:
path = / Users / krunal / Library / Application Support / iPhone Simulator / 5.0 / Applications / 9FF61238-2D1D-4CB7-8E24-9AC7CE9415BC / iStock kotak.app/companymaster.sql
-(void) openDB {
//---create database---
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK )
{
sqlite3_close(db);
NSAssert(0, @"Database failed to open.");
}
}
-(void) getAllRowsFromTableNamed: (NSString *) tableName {
//---retrieve rows---
NSString *qsql = @"SELECT * FROM recentquotes";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) ==
SQLITE_OK) {
NSLog(@"b4 while");
while (sqlite3_step(statement) == SQLITE_ROW)
{
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
[recentqotarray addObject:field1Str];
[field1Str release];
}
//---deletes the compiled statement from memory---
sqlite3_finalize(statement);
NSLog(@"recentqotarray=%@",recentqotarray);
}
}
我写了这个,当我检查我的日志时,我得到这样的“在查找数据”,我没有得到我的sql = ...
- (void) finddata
{
NSString *databasePath;
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
NSLog(@"in finddata");
if (sqlite3_open(dbpath, &db) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM recentquotes"];
NSLog(@"sql=%@",querySQL);
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(@"Inside recent quote table");
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSLog(@"Column name=%s",field1);
NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
[recentqotarray addObject:field1Str];
NSLog(@"array=%@",recentqotarray);
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
}
}
提前致谢
答案 0 :(得分:1)
在你的:
NSString *sql = [NSString stringWithFormat:@"INSERT INTO recentquotes ('symbol', 'dt_tm') VALUES ('%@',datetime())",symbol];
而不是'%@'
尝试使用\"%@\"
,并检查它是否插入到您的数据库中。
编辑:
我最近一直在研究数据库,并且我已经能够成功地在我的sqlite中插入数据,我会写下我使用的检查是否有帮助:
NSArray*dirPath;
NSString*docDir;
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docDir=[dirPath objectAtIndex:0];
databasePath=[docDir stringByAppendingPathComponent:@"example.sqlite"];
BOOL success;
NSFileManager*fm=[NSFileManager defaultManager];
success=[fm fileExistsAtPath:databasePath];
if(success)
{
NSLog(@"Already present");
}
NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"example" ofType:@"sqlite"];
NSError*error;
success=[fm copyItemAtPath:bundlePath toPath:databasePath error:&error];
if(success)
{
NSLog(@"Created successfully");
}
const char*dbPath=[databasePath UTF8String];
if(sqlite3_open(dbPath, &myDB)==SQLITE_OK)
{
NSString*insertSQL=[NSString stringWithFormat:@"insert into extable (name) values (\"%@\")",[nametextField.text]];
const char*insertStmt=[insertSQL UTF8String];
char *errmsg=nil;
if(sqlite3_exec(myDB, insertStmt, NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(@"ADDED!");
}
sqlite3_close(myDB);
}