Question

I have a .sql file which I have exported from MySQL. I am trying to add that table structure into SQLite using FMDB framework but it is not executed and always return false. Here is the structure:

CREATE TABLE IF NOT EXISTS `admin` (
`admin_id` tinyint(4) NOT NULL AUTO_INCREMENT,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(30) NOT NULL,
`date` datetime DEFAULT NULL,
`status` tinyint(2) NOT NULL,
PRIMARY KEY (`admin_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

What I am doing wrong here ?

Was it helpful?

Solution

That SQL doesn't conform to SQLite's CREATE TABLE syntax as documented online. If you look at the FMDB lastErrorMessage method (which is analogous to sqlite3_errmsg), it will report the specifics regarding the error.

Notably, the AUTO_INCREMENT syntax is not correct. Even if you fix that, the ENGINE and DEFAULT CHARSET options won't be recognized, either. At the very minimum, you'd have to try something like:

NSString *sql = @"CREATE TABLE IF NOT EXISTS `admin` ("
                "`admin_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
                "`first_name` varchar(30) NOT NULL,"
                "`last_name` varchar(30) NOT NULL,"
                "`email` varchar(30) NOT NULL,"
                "`password` varchar(30) NOT NULL,"
                "`date` datetime DEFAULT NULL,"
                "`status` tinyint(2) NOT NULL"
                ")";

Or, given that SQLite won't understand some of those data types (though with SQLite's "dynamic type system", it doesn't really matter), you might want to replace varchar(30) with TEXT and tinyint(2) with INTEGER and datetime with whatever format you plan on storing dates. See Data Types discussion.

OTHER TIPS

You are trying to use MySQL extensions in another database.

You should at least replace the admin_id type with INTEGER, replace AUTO_INCREMENT with AUTOINCREMENT, and remove the ENGINE and DEFAULT CHARSET settings.

  • (IBAction)findContact:(UIButton *)sender {

    const char *dbPath = [databasePath UTF8String]; sqlite3_stmt *statement;

    if (sqlite3_open(dbPath, &contactDB)== SQLITE_OK) { NSString *querySQL = [NSString stringWithFormat:@"SELECT ADDRESS,PHONE FROM CONTACTS WHERE NAME = \"%@\"",_name.text]; const char *query_stmt = [querySQL UTF8String]; if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)== SQLITE_OK) {

        if (sqlite3_step(statement)== SQLITE_ROW) {
    
            NSString *addressField = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
            _address.text = addressField;
            NSString *phoneField = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
            _phoneNo.text = phoneField;
            _status.text = @"Match Found";
        }else{
            _status.text = @"Match not found";
            _address.text = @"";
            _phoneNo.text = @"";
        }
        sqlite3_finalize(statement);
    }
    sqlite3_close(contactDB);
    }
    

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top