SQLite建立表並添加資料,sqlite建立資料

來源:互聯網
上載者:User

SQLite建立表並添加資料,sqlite建立資料

- (void)viewDidLoad {    [super viewDidLoad];    //建立表    [self creatTable];    //插入資料    [self insertTable];}// -----------------------建立一個表--------------------- (void)creatTable{// 1.建立一個資料庫物件    sqlite3 *sqlite3 = nil;    // 2.資料庫的路徑    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/mySqlite.db"];    // 3.開啟資料庫 (通過指定路徑開啟資料庫檔案,如果沒有就建立)    int result = sqlite3_open([path UTF8String], &sqlite3);    if (result != SQLITE_OK) {        NSLog(@"資料庫開啟失敗!");        return;    }    // 4.建立sql語句    NSString *sql = @"CREATE TABLE Students (id integer PRIMARY KEY,name text)";    // 5.執行SQL語句    char *error = NULL;    result = sqlite3_exec(sqlite3, [sql UTF8String], NULL, NULL, &error);    if (result != SQLITE_OK) {        NSLog(@"執行sql語句失敗!");        // 6.關閉資料庫        sqlite3_close(sqlite3);        return;    }// 6.關閉資料庫    sqlite3_close(sqlite3);}// -------------------------插入資料------------------------- (void)insertTable{// 1.建立一個資料庫物件    sqlite3 *sqlite3 = nil;    // 2.資料庫的路徑    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/mySqlite.db"];    // 3.開啟資料庫 (通過指定路徑開啟資料庫檔案,如果沒有就建立)    int result = sqlite3_open([path UTF8String], &sqlite3);    if (result != SQLITE_OK) {        NSLog(@"資料庫開啟失敗!");        return;    }    // 4.建立sql語句    // insert into students(id,name) values('123456','李斯')    NSString *sql = @"insert into students(id,name) values(?,?)";    // 5.編譯sql語句    // 建立一個資料控制代碼對象    sqlite3_stmt *stmt = nil;    result = sqlite3_prepare_v2(sqlite3, [sql UTF8String], -1, &stmt, nil);    if (result != SQLITE_OK) {        NSLog(@"編譯失敗");        // 關閉資料庫        sqlite3_close(sqlite3);        return;    }    // 6.綁定資料到資料控制代碼裡面    sqlite3_bind_int(stmt, 1, 123457);    sqlite3_bind_text(stmt, 2, "張三", -1, nil);    // 7.執行資料控制代碼的操作    result = sqlite3_step(stmt);    if (result == SQLITE_ERROR || result == SQLITE_MISUSE) {        NSLog(@"插入失敗");        // 關閉資料控制代碼        sqlite3_finalize(stmt);        // 關閉資料庫        sqlite3_close(sqlite3);        return ;    }    // 8.執行成功    NSLog(@"插入成功");    // 關閉資料控制代碼    sqlite3_finalize(stmt);    // 關閉資料庫    sqlite3_close(sqlite3);       }


android中的SQLite在建立資料庫、建立表後,怎給表增加列數,或者更新版本?

//定義升級函數
private void upgradeDatabaseToVersion1(SQLiteDatabase db) {
// Add 'new' column to mytable table.
db.execSQL("ALTER TABLE mytable ADD COLUMN new TEXT");
}

//重寫onUpgrade
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to " + currentVersion + ".");

switch (oldVersion) {
case 0:
if (currentVersion <= 1) {
return;
}

db.beginTransaction();
try {
upgradeDatabaseToVersion1(db);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(TAG, ex.getMessage(), ex);
break;
} finally {
db.endTransaction();
}

return;
}

Log.e(TAG, "Destroying all old data.");
dropAll(db);
onCreate(db);
}

-----------------------------------------------------------------------------------------------------
安卓精英團為你解答
安卓精英團歡迎各位精英加入
 
android中的SQLite在建立資料庫,並且建立表後,怎給表增加列數

ALTER TABLE download_info ADD state Integer
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.