您好,我正在开发一个带有 2 个表的数据库 dbmain,我的代码
NSString *docsDir;
NSArray *dirPaths;
dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// docsDir=[dirPaths objectAtIndex:0];
docsDir = dirPaths[0];
databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"dbDare.db"]];
NSFileManager *filemgr=[NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath]==NO)
{
const char *dbpath=[databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS tblUser(ID INTEGER PRIMARY KEY AUTOINCREMENT, StrName TEXT,Strusername TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create table"
message:@"Failed to create table"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//status.text = @"Failed to create table";
}
char *errMsg1;
const char *sql_stmt1 =
"CREATE TABLE IF NOT EXISTS tblVanish (ID INTEGER PRIMARY KEY AUTOINCREMENT, strfilepath TEXT,datesaved TEXT)";
if (sqlite3_exec(contactDB, sql_stmt1, NULL, NULL, &errMsg1) != SQLITE_OK)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create table"
message:@"Failed to create table"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
//status.text = @"Failed to create table";
}
sqlite3_close(contactDB);
} else {
// status.text = @"Failed to open/create database";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create open/create database"
message:@"Failed to create open/create database"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
将记录添加到表后,我可以使用 lita 软件查看行,但是通过此代码检索行时
const char *dbpath=[databasePath UTF8String];
int iVal=0;
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = @"SELECT Strusername FROM tblUser";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
while (sqlite3_step(statement) == SQLITE_ROW) {
strname = [NSString stringWithFormat:@"Welcome %@" ,[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)]];
}
} else {
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
我无法获取任何东西,因为使用 ios 的表内没有显示任何行,但我可以使用 lita 看到表内的行 请建议我在哪里弄错了