android 資料庫的基本操作
阿新 • • 發佈:2018-11-26
MainActivity.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="add" android:text="新增" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查詢" android:onClick="select" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="刪除" android:onClick="delete" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改" android:onClick="update" /> </LinearLayout>
======
MainActivity.java
package com.example.sqlite_demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import dao.UserDao; public class MainActivity extends Activity { private UserDao dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dao = new UserDao(this); } //點選新增 public void add(View v){ dao.add("wzq", "天津"); } //點選查詢 public void select(View v){ String select = dao.select(); Toast.makeText(MainActivity.this, select, Toast.LENGTH_SHORT).show(); } //刪除 public void delete(View v){ dao.delete("wzq"); } //修改 public void update(View v){ dao.update("wzq", "20", "wzq"); } }
======
UserDao.java
package dao; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import SqliteHelper.SqliteHelper; public class UserDao { private SqliteHelper sqliteHelper; private SQLiteDatabase database; public UserDao(Context context){ //建立資料庫 sqliteHelper = new SqliteHelper(context); database = sqliteHelper.getReadableDatabase(); } //增加 ? 佔位 public void add(String name,String age){ database.execSQL("insert into users (name,age) values (?,?)",new Object[]{name,age}); } //查詢 // public List<E> select(){} 將資料展示在ListView public String select(){ /** * 1。sql語句 * 2.條件 * Cursor 遊標 */ Cursor rawQuery = database.rawQuery("select * from users", null); StringBuffer stringBuffer = new StringBuffer(); while (rawQuery.moveToNext()) { //得到資料庫的值 String name = rawQuery.getString(rawQuery.getColumnIndex("name")); String age = rawQuery.getString(rawQuery.getColumnIndex("age")); stringBuffer.append(name+age); } return stringBuffer.toString(); } //刪除 public void delete(String name){ database.execSQL("delete from users where name = ? ",new Object[]{name}); } //修改 update users set name = ?,age = ? 修改的值 where name = ? 條件 public void update(String name,String age,String tj){ database.execSQL("update users set name = ?,age = ? where name = ?",new Object[]{name,age,tj}); } }
========
SqliteHelper.java
package SqliteHelper; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class SqliteHelper extends SQLiteOpenHelper { /** * * @param context 上下文 * @param name 資料庫的名字 * @param factory 工廠遊標 * @param version 版本 */ public SqliteHelper(Context context) { super(context,"User.db", null, 1); } //建立表 成功建立只能執行一次 @Override public void onCreate(SQLiteDatabase db) { db.execSQL( "create table users(id integer primary key autoincrement," + "name text," + "age text)"); } //升級資料庫的方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }