SQLite 使用(創、升、增、刪、改、查)
阿新 • • 發佈:2017-12-06
ted phone ces @override 設置 activity color void dmi
首先Database 數據庫創建
1.創建一個Database包,包內創建一個MyDatabaseHelper繼承DatabaseHelper類,並實現裏面的方法!
3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 import android.widget.Toast; 7 8 /** 9 * 數據庫幫助類 10 * Created by Administrator on 2017/12/6.11 */ 12 13 public class MyDatabaseHelper extends SQLiteOpenHelper{ 14 15 public static final String CREATE_USERINFO="CREATE TABLE USERINFO(" //創建表的SQL語句 16 + "id integer primary key autoincrement, " //id 設置主鍵 17 + "username text, " //用戶名 18 + "age integer, " //年齡 19 + "sex text, " //性別 20 + "mAutograph text)"; //簽名 21 private Context mContext;22 23 24 /** 25 * 生成的構造方法 26 * @param context 獲得上下文 27 * @param name 28 * @param factory 29 * @param version 30 */ 31 public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 32 super(context, name, factory, version); 33 mContext=context; //獲取上下文 34 } 35 36 37 /** 38 * 實現方法 39 * 創建數據庫 40 * @param db 41 */ 42 @Override 43 public void onCreate(SQLiteDatabase db) { 44 db.execSQL(CREATE_USERINFO); //創建數據庫,參數為創表語句 45 Toast.makeText(mContext,"創建表成功",Toast.LENGTH_LONG).show(); 46 } 47 48 /** 49 * 實現方法 50 * 升級數據庫 51 * @param sqLiteDatabase 52 * @param i 53 * @param i1 54 */ 55 @Override 56 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 57 58 } 59 }
2.升級數據庫,
1 /** 2 * 實現方法 3 * 升級數據庫 4 * @param sqLiteDatabase 5 * @param i 6 * @param i1 7 */ 8 @Override 9 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 10 sqLiteDatabase.execSQL("drop table if exists CREATE_USERINFO"); //刪除數據庫 11 sqLiteDatabase.execSQL("drop table if exists CREATE_CONTACT"); //刪除數據庫 12 } 13 14 private MyDatabaseHelper mDBhelper; //聲明數據庫幫助類變量 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 mDBhelper=new MyDatabaseHelper(this,"this,UserInfo",null,3); //3代表升級 20 } 25 /** 26 * 啟動時創建數據庫 27 */ 28 @Override 29 protected void onStart() { 30 super.onStart(); 31 mDBhelper.getWritableDatabase(); //獲得可寫的數據庫,會創建helper數據庫 32 }
3.查詢數據庫
1 @Override 2 public List<UserInfo> getUserInfo(MyDatabaseHelper db) { 3 SQLiteDatabase sqlhelper=db.getWritableDatabase(); 4 Cursor cursor=sqlhelper.query("USERINFO",null,null,null,null,null,null); 5 UserInfo user; 6 List<UserInfo> list=new ArrayList<>(); 7 if(cursor.moveToFirst()){ 8 do{ 9 String name=cursor.getString(cursor.getColumnIndex("username")); 10 String age=cursor.getString(cursor.getColumnIndex("age")); 11 String sex=cursor.getString(cursor.getColumnIndex("sex")); 12 String phone=cursor.getString(cursor.getColumnIndex("Phone")); 13 user=new UserInfo(name,age,sex,phone); 14 list.add(user); 15 }while (cursor.moveToNext()); 16 } 17 cursor.close(); 18 return list; 19 }
4.插入數據庫 MyDatabaseHelper database繼承至DatabaseHelper
1 @Override 2 public void setUserInfo(MyDatabaseHelper database,UserInfo user) { 3 SQLiteDatabase db=database.getWritableDatabase(); 4 ContentValues values=new ContentValues(); 5 values.put("username",user.getName()); 6 values.put("age",user.getAge()); 7 values.put("sex",user.getSex()); 8 values.put("Phone",user.getPhone()); 9 db.insert("USERINFO",null,values); 10 }
5.修改數據庫
1 @Override 2 public void DelectUserInfo(MyDatabaseHelper database) { 3 SQLiteDatabase sqLiteDatabase=database.getWritableDatabase(); 4 ContentValues values=new ContentValues(); 5 values.put("price",10.99); //所需要修改的字段 price 字段 10.99值 6 7 //修改數據 ?站位符 new String[]{修改?數據} 8 sqLiteDatabase.update("USERINFO",values,"name=?",new String[]{"TheDaviciCode"}); 9 }
6.刪除數據
1 @Override 2 public void DelectUserInfo(MyDatabaseHelper database) { 3 //刪除ID小於5的數據 4 SQLiteDatabase db=database.getWritableDatabase(); 5 db.delete("USERINFO","id > 5",new String[]{"5"}); 6 }
SQLite 使用(創、升、增、刪、改、查)