日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

sqlite自己主动更新数据库

發(fā)布時間:2024/4/15 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqlite自己主动更新数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

寫一個類繼承自 ?SQLiteOpenHelper

系統(tǒng)會自己主動加入構造方法、 onCreate方法、onUpgrade方法

當數(shù)據庫里面數(shù)據或者表結構有所修改時。咱們須要升級數(shù)據庫

這個時候。版本號加1.在update里面做對應改動。

須要注意的是,假設須要測試update,每次開始測試,version 值增大。假設和上次的同樣。就不會促發(fā)update方法了

以下貼上代碼

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.widget.Toast;
//第一版的時候,update里面的代碼為空。由于不須要更新,之后版本號更新,就得在update里面寫代碼了

public class DataHelp extends SQLiteOpenHelper {


public static String name = "sxjj.db3";
public static int version = 3;
Context context = null;


public DataHelp(Context context, String name, CursorFactory factory,
int version) {


super(context, name, null, version);
this.context = context;
}


/**
* 創(chuàng)建表
*/
public void onCreate(SQLiteDatabase db) {
// 更改表結構之前的
// String sql_tansInfo =
// "create table if not exists transInfo(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int )";
// 更改表結構之后的,加入了兩列。marker 和address
String sql_tansInfo = "create table if not exists transInfo(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int,address text,marker text )";
db.execSQL(sql_tansInfo);


}


/**
* 更新表
*/
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// 第一次執(zhí)行。版本號加1的時候,觸發(fā)這種方法了,下一次就不觸發(fā)了,所以每次測試須要版本號號加1,正式公布不須要


// 1創(chuàng)建新表
String sql_create_tansInfo = "create table if not exists transInfo_new(id integer primary key autoincrement, name nvarchar(50), tel nvarchar(20), content text,type int,address text,marker text )";


db.execSQL(sql_create_tansInfo);


// 2 刪除舊表
String sql_drop_oldTable = "drop table transInfo";
db.execSQL(sql_drop_oldTable);


// 3表新表的名字改成舊表的名字
String sql_rename = "ALTER TABLE ?transInfo_new RENAME TO transInfo";
db.execSQL(sql_rename);


// 提示更新了,正式公布可去掉這一句提示
Toast.makeText(context, "更新---------", 1).show();


// 注:假設僅僅是添加一列,事實上不須要創(chuàng)建新表。
// 運行 String sql="ALTER TABLE transInfo ADD COLUMN address text;";
// 就能夠了。上述的方法是一個通用的方法


}


}


轉載于:https://www.cnblogs.com/cxchanpin/p/6889300.html

總結

以上是生活随笔為你收集整理的sqlite自己主动更新数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。