安卓的sqlite增刪改

來源:互聯網
上載者:User

標籤:

基於安卓的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增刪改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.