SQLite的CRUD

前端之家收集整理的这篇文章主要介绍了SQLite的CRUD前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. packagecom.wansha;
  2. importandroid.app.Activity;
  3. importandroid.content.ContentValues;
  4. importandroid.database.Cursor;
  5. importandroid.database.sqlite.sqliteDatabase;
  6. importandroid.database.sqlite.sqliteOpenHelper;
  7. importandroid.os.Bundle;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.Button;
  11. importcom.wansha.db.DatabaseHelper;
  12. publicclassActivity12extendsActivity{
  13. privateButtoncreateDatabase;
  14. privateButtonupdateDatabase;
  15. privateButtoninsert;
  16. privateButtonupdate;
  17. privateButtonread;
  18. privateButtondelete;
  19. /**Calledwhentheactivityisfirstcreated.*/
  20. @Override
  21. voidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. this.createDatabase=(Button)this.findViewById(R.id.createDatabase);
  25. this.createDatabase.setOnClickListener(newCreateDatabaseListener());
  26. this.updateDatabase=(Button)this.findViewById(R.id.updateDatabase);
  27. this.updateDatabase.setOnClickListener(newUpdateDatabaseListener());
  28. this.insert=(Button)this.findViewById(R.id.insert);
  29. this.insert.setOnClickListener(newInsertListener());
  30. this.update=(Button)this.findViewById(R.id.update);
  31. this.update.setOnClickListener(newUpdateListener());
  32. this.read=(Button)this.findViewById(R.id.read);
  33. this.read.setOnClickListener(newReadListener());
  34. this.delete=(Button)this.findViewById(R.id.delete);
  35. this.delete.setOnClickListener(newDeleteListener());
  36. }
  37. classCreateDatabaseListenerimplementsOnClickListener{
  38. voidonClick(Viewv){
  39. sqliteOpenHelpersqlite=newDatabaseHelper(Activity12.this,"sharp");
  40. sqlite.getReadableDatabase();
  41. }
  42. classUpdateDatabaseListenerimplementsOnClickListener{
  43. @Override
  44. voidonClick(Viewv){
  45. System.out.println("UpdateDatabaseListenerisinvoked!!!");
  46. "sharp",2);
  47. classInsertListener sqliteOpenHelpersqlite="sharp");
  48. sqliteDatabasedatabase=sqlite.getWritableDatabase();
  49. ContentValuesvalues=newContentValues();
  50. values.put("username","xiaopeng");
  51. database.insert("sharp",null,values);
  52. classUpdateListener"pengpeng");
  53. database.update("sharp",values,"id=?",153); background-color:inherit; font-weight:bold">newString[]{"2"});
  54. classReadListener Cursorcursor=database.query("sharp",153); background-color:inherit; font-weight:bold">newString[]{"id","username"},153); background-color:inherit; font-weight:bold">null);
  55. while(cursor.moveToNext()){
  56. Stringusername=cursor.getString(cursor.getColumnIndex("username"));
  57. System.out.println("username---->"+username);
  58. classDeleteListener database.delete("sharp",153); background-color:inherit; font-weight:bold">newString[]{"1"});
  59. }

main.xml

[html] ?
    <?xmlversion="1.0"encoding="utf-8"?>
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical">
  5. Buttonandroid:id="@+id/createDatabase"
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:text="创建数据库"
  9. />
  10. Buttonandroid:id="@+id/updateDatabase"
  11. android:layout_height="wrap_content"
  12. android:text="更新数据库"
  13. />
  14. Buttonandroid:id="@+id/insert"
  15. android:text="插入数据"
  16. Buttonandroid:id="@+id/update"
  17. android:text="更新数据"
  18. Buttonandroid:id="@+id/read"
  19. android:text="读取数据"
  20. Buttonandroid:id="@+id/delete"
  21. android:text="删除数据"
  22. </LinearLayout>


db.execsql("create table sharp(id integer primary key autoincrement,username varchar(255))");

创建自增长ID


DatebaseHelper.java

?
    packagecom.wansha.db;
  1. importandroid.content.Context;
  2. importandroid.database.sqlite.sqliteDatabase.CursorFactory;
  3. importandroid.database.sqlite.sqliteOpenHelper;
  4. classDatabaseHelperextendssqliteOpenHelper{
  5. privatestaticfinalintVERSION=1;
  6. publicDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory,
  7. intversion){
  8. super(context,name,factory,version);
  9. intversion){
  10. this(context,version);
  11. voidonCreate(sqliteDatabasedb){
  12. System.out.println("createtableisinvoked!!!");
  13. db.execsql("createtablesharp(idintegerprimarykeyautoincrement,usernamevarchar(255))");
  14. voidonUpgrade(sqliteDatabasedb,153); background-color:inherit; font-weight:bold">intoldVersion,153); background-color:inherit; font-weight:bold">intnewVersion){
  15. System.out.println("updatetableisinvoked!!!");
  16. }



原文链接:https://www.f2er.com/sqlite/201746.html

猜你在找的Sqlite相关文章