sqlite的增删改查操作

来源:互联网 发布:mac 虚拟机 游戏 编辑:程序博客网 时间:2024/06/14 12:55

sqlite是android 轻量级数据库

主要是两个类实现创建数据库和数据的增删改查


这个类是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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {// 创建数据库,这个方法会调用MyDatabaseHelper中的onCreate()方法和onUpgrade()方法dbHelper.getWritableDatabase();}});//添加数据Button Add_date = (Button) findViewById(R.id.Add_date);Add_date.setOnClickListener(new OnClickListener() {@Overridepublic 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", null, values);values.clear();//开始组装第二条数据values.put("name", "leoveo");values.put("author", "Frown Brown");values.put("pages", "500");values.put("price", "13.6");//插入第一条数据db.insert("Book", null, values);}});//更新数据Button Update_data = (Button) findViewById(R.id.Update_data);Update_data.setOnClickListener(new OnClickListener() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();//查询Book表中所有的数据Cursor cursor = db.query("Book", null, null, null, null, null, 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;}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_BOOK);db.execSQL(CREATE_CATEGORY);Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();}//表的升级 @Overridepublic 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>




0 0