开发者

adding new entry in sqlite database table

Hi I have written this code for adding new entry in database table. But while running it shows message that no table found. Please help me if u find any mistake in this code.

char *sqlStatement;
sqlite3 *pDb;
char *errorMsg;
ןnt returnCode;
char *databaseName;
databaseName = "Child.db";
returnCode = sqlite3_open(databaseName, &pDb);
if(returnCode!=SQLITE_OK) {
    fprintf(stderr, "Error in opening the database. Error: %s",
            sqlite3_errmsg(pDb));
    sqlite3_close(pDb);
    return;
}
else{
    NSLog(@"Open Sucessful");
}

//sqlStatement = sqlite3_mprintf("INSERT INTO childname VALUES"
//                             " ('%s')",[strTxtFldValue cString],"childname");
sqlStatement = sqlite3_mprintf("INSERT INTO childname VALUES  ('%s')",strTxtFldValue);
NSLog(@"sqlStatement....%c",sqlStatement);
returnCode = 开发者_开发百科sqlite3_exec(pDb, sqlStatement, 0, 0, &errorMsg);
NSLog(@"return code %d",returnCode);
if(returnCode!=SQLITE_OK) {
    fprintf(stderr,
            "Error in inserting into the stocks table. Error: %s", errorMsg);
    sqlite3_free(errorMsg);
}
else{
    NSLog(@"inserted");
}
sqlite3_free(sqlStatement); 


Try to create runtime database.

sqlite3_exec(database,
                 "CREATE TABLE IF NOT EXISTS PersonalDetailsTbl (ID INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,email TEXT,phoneNo TEXT,DOB TEXT,spouseName TEXT,aniversaryDate TEXT)",
                 NULL, NULL, NULL);

sqlite3_exec(database, [[NSString stringWithFormat:
                         @"INSERT INTO PersonalDetailsTbl VALUES(NULL, '%@','%@','%@','%@','%@', '%@')", 
                         name,email,phoneNo,DOB,spouseName,aniversaryDate]
                        UTF8String],
             NULL, NULL, NULL);


@user804417 How are u creating the Database table? If u are creating it manually and then adding the database file in the resouce folder, so that may work in simulator, but may give u problem when u run your app on device, so thats the one possibility of getting error like NO TABLE FOUND.

Inorder to resolve that try to create the database from withing the application when u lounch the app, this will definately resolve your problem.


If you execute same insert query from your sqlite software, you will get the same result. So there may be a mistake in giving table name in your app. Or may be you are giving wrong database name...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜