Android:数据库增删改查、SQLite、ORM、Cursor
生活随笔
收集整理的這篇文章主要介紹了
Android:数据库增删改查、SQLite、ORM、Cursor
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.繼承類SQLiteOpenHelper:
public class MySQLiteHelper extends SQLiteOpenHelper {//重寫構造方法public MySQLiteHelper(Context context, String name, CursorFactory factory,int version){super(context, name, factory, version);}//創建表@Overridepublic void onCreate(SQLiteDatabase db){db.execSQL("CREATE TABLE person(personid INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))" );db.execSQL("CREATE TABLE student(personid INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))" );}//升級表(當Database的Version低于當前new里的Version,直接執行下面方法)@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){int v = newVersion - oldVersion;switch (v){case 3:db.execSQL("ALTER TABLE person ADD salary3 VARCHAR(20)");case 2:db.execSQL("ALTER TABLE person ADD salary2 VARCHAR(20)");case 1:db.execSQL("ALTER TABLE person ADD salary1 VARCHAR(20)");default:break;}} }2.實現增刪改查:
/*** CREATE TABLE person (personid INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20) ) DROP TABLE person //刪除表 INSERT INTO person(name,phone) VALUES ("rongqin","1598088588") //插入 UPDATE person SET name="Andfeel" WHERE personid=2 //改 ALTER TABLE person ADD salary //增加列 DELETE FROM person WHERE name="Andfeel" //刪除SELECT * FROM person WHERE name="Andfeel" SELECT phone,name FROM person SELECT name,phone FROM person ORDER BY name ASC"//ASC升序 DESC降序 **/ public class MainActivity extends Activity implements OnClickListener {private TextView textView;private MySQLiteHelper mDBHelper;private MySQLiteHelper bHelper;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn__create_table).setOnClickListener(this);findViewById(R.id.btn_delete_table).setOnClickListener(this);findViewById(R.id.btn_alter_table).setOnClickListener(this);findViewById(R.id.btn_insert).setOnClickListener(this);findViewById(R.id.btn_delete).setOnClickListener(this);findViewById(R.id.btn_update).setOnClickListener(this);findViewById(R.id.btn_select).setOnClickListener(this);textView = (TextView) findViewById(R.id.textView1);//創建數據庫mDBHelper = new MySQLiteHelper(this, "one.db", null, 3);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overridepublic void onClick(View v){switch (v.getId()){case R.id.btn__create_table:btnCreateTable();break;case R.id.btn_delete_table:btnDeleteTable();break;case R.id.btn_alter_table:btnAlterTable();break;case R.id.btn_insert:btnInsert();break;case R.id.btn_delete:btnDelete();break;case R.id.btn_update:btnUpdate();break;case R.id.btn_select:btnSelect();break;default:break;}}private void btnSelect()//查{SQLiteDatabase db = mDBHelper.getWritableDatabase();// Cursor cursor = db.rawQuery("SELECT * FROM person WHERE name = ?", new String[]{"rongqin"}); // cursor.moveToFirst();Cursor cursor = db.query("person", //表名new String[]{"phone","name"}, //查詢的列名"name=?", //查詢條件new String[]{"huangxx"}, //查詢參數null, null,"phone DESC"); //排序,升序為ASC,降序為DESCboolean hasdata = cursor.moveToFirst();//游標移到第一行StringBuilder sb = new StringBuilder();while(hasdata)//是否有數據{int columnIndex = cursor.getColumnIndex("name");//得到name的列數String name = cursor.getString(columnIndex);//得到name這一列的值int columnIndex2 = cursor.getColumnIndex("phone");String phone = cursor.getString(columnIndex2);sb.append("name:").append(name).append(" phone:").append(phone).append("--");hasdata = cursor.moveToNext();//游標移動到下一行,判斷是否有值}textView.setText(sb.toString());}private void btnUpdate()//改{SQLiteDatabase db = mDBHelper.getWritableDatabase(); // db.execSQL("UPDATE person SET phone='0592xxxxxx' WHERE personid=5");ContentValues values = new ContentValues();values.put("name", "haoyouduo");values.put("phone", "666666");db.update("person", values , "personid=?" , new String[]{"8"});}private void btnDelete()//刪{SQLiteDatabase db = mDBHelper.getWritableDatabase(); // db.execSQL("DELETE FROM person WHERE name='Andfeel'");db.delete("person","name = ? and personid = ?", new String[]{"haoyouduo" , "5"});}private void btnInsert()//增{SQLiteDatabase db = mDBHelper.getWritableDatabase(); // db.execSQL("INSERT INTO person(name,phone) VALUES ('rongqin',1598088588)");ContentValues values = new ContentValues();values.put("name", "huangxx");values.put("phone", "158888");db.insert("person", null , values );}private void btnAlterTable()//擴展表{}private void btnDeleteTable()//刪除表{}private void btnCreateTable()//創建表{} }其他總結:
1.關閉數據源
/*** 關閉數據源*/ public void closeConnection() {if (mDb != null && mDb.isOpen())mDb.close();if (mDbHelper != null)mDbHelper.close(); }轉載于:https://blog.51cto.com/glblong/1216877
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Android:数据库增删改查、SQLite、ORM、Cursor的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python抓取中文网页乱码
- 下一篇: Dump与Win Program Cra