如何使用sqlite数据库插入数据?

时间:2016-04-21 09:09:29

标签: ios

- (void)viewDidLoad {
    // Do any additional setup after loading the view, typically from a nib.

    NSString *docsDir;

    NSArray *dirPaths;

    dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

    docsDir =[dirPaths objectAtIndex:0];

    databasePath=[[NSString alloc]initWithString: [docsDir stringByAppendingString:@"test2.db"]];

    //databasePath=[NSString stringWithFormat:[docsDir stringByAppendingPathComponent:@"test2.db"]];

    NSFileManager *filemgr=[NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath:databasePath] ==NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &test2DB) ==SQLITE_OK)
        {
            char *errMSg;

            const char *sql_stmt= "CREATE TABLE IF NOT EXISTS TESTING (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, EMAIL TEXT, ADDRESS TEXT)";

            if (sqlite3_exec(test2DB, sql_stmt, NULL, NULL, &errMSg) != SQLITE_OK)
            {
                status.text=@"Failed To Create Table";
            }
            sqlite3_close(test2DB);
        }else{
            status.text =@"Failed to open/create database";
        }
    }
    [super viewDidLoad];

}

//保存数据

-(IBAction)save:(id)sender
{
    sqlite3_stmt *statment;

    const char *dbpath =[databasePath UTF8String];

    if (sqlite3_open(dbpath, &test2DB) ==SQLITE_OK) {
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO TESTING(Firstname,Lastname,Email,Address) VALUES (\"%@\", \"%@\", \"%@\", \%@\")",Firstname.text,Lastname.text,Email.text,Address.text];

        const char *insert_stmt=[insertSQL UTF8String];
        sqlite3_prepare_v2(test2DB, insert_stmt, -1, &statment, NULL);

        if (sqlite3_step(statment) ==SQLITE_DONE) {
            status.text=@"Details Added";
            Firstname.text=@"";
            Lastname.text=@"";
            Email.text=@"";
            Address.text=@"";
        }else
        {
            status.text=@"Failed to Add Details";
        }
        sqlite3_finalize(statment);
        sqlite3_close(test2DB);
    }
}

2 个答案:

答案 0 :(得分:1)

不是b。这是<to uri="log:like-to-see-all?level=INFO&amp;showAll=true&amp;multiline=true"/>

答案 1 :(得分:0)

将数据库名称更改为test2.sqlite,NSDocumentDirectory并使用此代码保存您的数据:

- (IBAction) saveData
{
    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &Information) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO CONTACTS (name, address, phone,imageUrl) VALUES (\"%@\", \"%@\", \"%@\",\"%@\")", name.text, address.text, phone.text,imageUrl.text];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(Information, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            status.text=@"Details Added";
        Firstname.text=@"";
        Lastname.text=@"";
        Email.text=@"";
        Address.text=@""
        } else {
            status.text = @"Failed to add contact";
        }
        sqlite3_finalize(statement);
        sqlite3_close(test2);
    }
}