开发者

"SQL error or missing database" error while preparing statement

开发者 https://www.devze.com 2023-01-08 14:51 出处:网络
I am trying to insert values in database but while preparing statement it is giving me error \"SQL error or missing database\"...can anyone know what this error is and how one can resolve this?????my

I am trying to insert values in database but while preparing statement it is giving me error "SQL error or missing database"...can anyone know what this error is and how one can resolve this?????my path for database is

2010-07-22 14:34:59.933 DatabaseApp[1521:207] path of database /Users/nuzhat/Library/Application Support/iPhone Simulator/User/Applications/C50A0188-2A9A-487F-951C-6E7FFCE3CFBB/Documents/UserName.db3

here is my code:- // Open the database connection and retrieve minimal information for all objects. - (void)initializeDatabase {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TextWandiPhone.db3"];

//for second database
NSString *path1 = [documentsDirectory stringByAppendingPathComponent:@"UserName.db3"];


// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
{
    // Get the primary key for all books.
    const char *sql = "SELECT CountryName FROM Country";

    sqlite3_stmt *statement = nil;
    // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
    // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.        
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
    {
    //  int success=sqlite3_step(statement);
        // We "step" through the results - once for each row.
        while(sqlite3_step(statement) == SQLITE_ROW) 
        {
    //  if(success == SQLITE_ROW) {
            //NSLog(@"value of success %d",success);

            // The second parameter indicates the column index into the result set.
            char *str = (char *)sqlite3_column_text(statement, 0);
            NSString *country = (str) ? [NSString stringWithUTF8String:str] : @"";
            //NSLog(@"value :%@",country);
                            //NSLog(@"after running query");

            //NSLog(@"after initializing array");
            [arrCountry addObject:country];
            //NSLog(@"after adding values");
            //[return arrCountry];

        }//while
        //NSLog(@"values of array %@",arrCountry);
    }//aft prepare

    sqlite3_finalize(statement);
}//aft open

    //for second database
    //else if (sqlite3_open([path1 UTF8String], &database1) == SQLITE_OK) 
if (sqlite3_open([path1 UTF8String], &database1) == SQLITE_OK) 
    {

        // Get the primary key for all books.
        const char *sql1 = "SELECT UserID FROM User";

        sqlite3_stmt *statement1=nil;
        // Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
        // The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.
        int value1=sqlite3_prepare_v2(database1, sql1, -1, &statement1, NULL);
        NSLog(@"value of preparing stmt %d",value1);
        if ( value1== SQLITE_OK) 
        {
            //  int success=sqlite3_step(statement);
            // We "step" through the results - once for each row.
            while(sqlite3_step(statement1) == SQLITE_ROW) 
            {
                //  if(success == SQLITE_ROW) {
            //NSLog(@"value of success %d",success);

                // The second parameter indicates the column index into the result set.
                int userid = sqlite3_column_int(statement1, 0);
                //NSString *country = (str) ? [NSString stringWithUTF8String:str] : @"";
                //NSLog(@"value :%@",country);
                //NSLog(@"after running query");

                //NSLog(@"after initializing array");

                NSString *strUserId = [[NSNumber numberWithInt:userid] stringValu开发者_Go百科e];


                [arrUser addObject:strUserId];


                //[return arrCountry];
            }
        NSLog(@"values of array %@",arrUser);
        }//aft prepare

    // "Finalize" the statement - releases the resources associated with the statement.

        //sqlite3_finalize(statement1);
}//aft opening 2nd database 

else {
    // Even though the open failed, call close to properly clean up resources.
    sqlite3_close(database);
    sqlite3_close(database1);
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database1));
    // Additional error handling, as appropriate...
}

}

thanks in advance....


Not enough information here, but I think it could be a couple of things. Does the user of the database you are using have the permissions that allow them to insert data to the database? You might want to append your post so it includes the SQL statement you are using.

0

精彩评论

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