標籤:
基於安卓的sqlite增刪改,筆記學習:
1、使用LinearLayout 布局產生,增刪改的頁面
代碼布局如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 <Button android:onClick="click_add" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content"10 android:text="添加"11 />12 13 <Button android:onClick="click_delete" 14 android:layout_width="wrap_content"15 android:layout_height="wrap_content"16 android:text="刪除"17 />18 19 <Button android:onClick="click_update" 20 android:layout_width="wrap_content"21 android:layout_height="wrap_content"22 android:text="修改"23 />24 <Button android:onClick="click_search" 25 android:layout_width="wrap_content"26 android:layout_height="wrap_content"27 android:text="查詢"28 />29 30 31 </LinearLayout>
後台代碼:
1 private MySqliteHelper helper; 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 helper = new MySqliteHelper(getApplicationContext()); 7 //開啟或者建立資料庫 第一次 8 SQLiteDatabase data=helper.getWritableDatabase(); 9 //開啟或者建立資料庫 第一次 磁碟滿了就返回唯讀資料庫10 //SQLiteDatabase data=helper.getReadableDatabase();11 12 }13 //新增14 public void click_add(View v){15 SQLiteDatabase db=helper.getWritableDatabase();16 for (int i = 0; i < 10; i++) {17 db.execSQL("INSERT into table_info(name,pwd) values(‘使用者"+i+"‘,‘1‘)");18 }19 Toast.makeText(MainActivity.this, "建立完成1000條記錄", 0);20 21 }22 //修改23 public void click_update(View v){24 SQLiteDatabase db=helper.getWritableDatabase();25 db.execSQL("update table_info set pwd=‘0000‘");26 }
2、MySqliteHelper類說明
1 public class MySqliteHelper extends SQLiteOpenHelper { 2 3 //自訂訪問sqlite 4 public MySqliteHelper(Context context) { 5 super(context, "CarDb.db", null, 3); 6 // TODO Auto-generated constructor stub 7 } 8 9 @Override10 public void onCreate(SQLiteDatabase db) {11 // TODO Auto-generated method stub12 System.out.print("開始建立資料庫..");13 db.execSQL("create table table_info (_id integer primary key autoincrement,name varchar(20),pwd varchar(50))");14 15 }16 17 @Override18 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {19 // TODO Auto-generated method stub20 db.execSQL("alter table table_info add status varchar(2)");21 }
學習筆記記錄,點擊按鈕往資料庫添加和修改資料。
安卓的sqlite增刪改