主要是两个类实现创建数据库和数据的增删改查
这个类是MainActivity 是创建数据库对象,通过这个对象dbHelper调用getWritableDatabase()方法就可以去创建数据库了
接下来就是对数据库的增删改查操作。getWritableDatabase()这个方法很重要 数据库的创建升级增删改查都需要这个方法。ContentValues()这个方法是用来添加数据的。
package com.example.sqlitetest; import com.example.sqlitetest.R; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private MyDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //生成数据库对象 dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,4); Button createDatabase = (Button) findViewById(R.id.create_database); createDatabase.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建数据库,这个方法会调用MyDatabaseHelper中的onCreate()方法和onUpgrade()方法 dbHelper.getWritableDatabase(); } }); //添加数据 Button Add_date = (Button) findViewById(R.id.Add_date); Add_date.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //getWritableDatabase()方法会返回一个sqliteDatabase对象,这个 //对象可以对数据库进行增删改查的操作 sqliteDatabase db = dbHelper.getWritableDatabase(); //通过这个方法添加数据 ContentValues values = new ContentValues(); //开始组装第一条数据 values.put("name","sunning"); values.put("author","Dan Brown"); values.put("pages","500"); values.put("price","16.6"); //插入第一条数据 db.insert("Book",values); values.clear(); //开始组装第二条数据 values.put("name","leoveo"); values.put("author","Frown Brown"); values.put("pages","13.6"); //插入第一条数据 db.insert("Book",values); } }); //更新数据 Button Update_data = (Button) findViewById(R.id.Update_data); Update_data.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sqliteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",66.6); db.update("Book",values,"name = ?",new String[]{"sunning"}); } }); //删除数据 Button Delete_data = (Button) findViewById(R.id.Delete_data); Delete_data.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sqliteDatabase db = dbHelper.getWritableDatabase(); db.delete("Book","pages>= ?",new String []{"500"}); } }); //查询数据 Button Query_data = (Button) findViewById(R.id.Query_data); Query_data.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sqliteDatabase db = dbHelper.getWritableDatabase(); //查询Book表中所有的数据 Cursor cursor = db.query("Book",null ); //cursor.moveToFirst() 表示是否指针移动到第一行的位置 if(cursor.moveToFirst()){ do{ //遍历Cursor对象,取出数据并打印,注意写的时候不要写错字符,要不然报错 String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); Double price = cursor.getDouble(cursor.getColumnIndex("price")); Log.d("MainActivity","name=="+name); Log.d("MainActivity","author=="+author); Log.d("MainActivity","pages=="+pages); Log.d("MainActivity","prices=="+price); }while(cursor.moveToNext()); } cursor.close(); } }); } }
下面这个类给我的感觉像一个工具类。就是继承sqliteOpenHelper 这个类 ,覆写onCreate()方法和onUpGrade()方法,对表进行创建和升级操作。还有就是创建表的字段语句
import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteDatabase.CursorFactory; import android.database.sqlite.sqliteOpenHelper; import android.widget.Toast; public class MyDatabaseHelper extends sqliteOpenHelper { //创建表 书 public static final String CREATE_BOOK = "create table Book ("+"id integer primary key autoincrement," +"author text,"+"price real,"+"pages integer,"+"name text)"; //创建表 书的分类 public static final String CREATE_CATEGORY = "create table Category(" +"id integer primary key autoincrement," +"category_name text," +"categoty_code integer)" ; private Context mContext; public MyDatabaseHelper(Context context,String name,CursorFactory factory,int version) { super(context,name,factory,version); mContext = context; } @Override public void onCreate(sqliteDatabase db) { db.execsql(CREATE_BOOK); db.execsql(CREATE_CATEGORY); Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show(); } //表的升级 @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { db.execsql("drop table if exists Book"); db.execsql("drop table if exists Category"); //在升级的方法里面重新创建表 onCreate(db); } }
<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" tools:context="com.example.sqlitetest.MainActivity" android:orientation="vertical" > <Button android:id="@+id/create_database" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Create_database" /> <Button android:id="@+id/Add_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Add_date" /> <Button android:id="@+id/Update_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Update_data" /> <Button android:id="@+id/Delete_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Delete_data" /> <Button android:id="@+id/Query_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Query_data" /> </LinearLayout>原文链接:https://www.f2er.com/sqlite/198942.html