Android-SQLite

[返回目录]


SQLite 是一个轻量级数据库,大小才几十K,但是麻雀虽小,五脏俱全;

官方网址:http://www.sqlite.org

与Java的JDBC和ODBC不同,SQLite的增删改查是通过SQLiteOpenHelper和SQLiteDatabase进行的;


SQLiteOpenHelper使用方法

getReadableDatabase() 返回一个可读的SQLiteDatabase

getWriteableDatabase() 返回一个可写的SQLiteDatabase

onCreate(SQLiteDatabase db)

onOpen(SQLiteDatabase db)

onUpgrade(SQLiteDatabase db,int old)


与其他组件不同额事,SQLiteOpenHelper的onCreate方法并不是创建的时候会调用,而是当调用getReadableDatabase()或者getWriteableDatabase()时会被调用(只第一次的时候调用)


在使用SQLite时,需编写一个类继承SQLiteOpenHelper并实现其构造方法、onCreate、OnUpdate等方法;

如下:

DatabaseHelper.java

package com.example.testsqlite;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

	private  static int VERSION=1;	//数据库的版本

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version, DatabaseErrorHandler errorHandler) {
		super(context, name, factory, version, errorHandler);
	}

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	public DatabaseHelper(Context context,String name,int version){
		this(context, name, null, version, null);
	}
	
	public DatabaseHelper(Context context,String name){
		super(context,name,null,VERSION,null);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql="create table user(id int,name varchar(20))";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		//当构造方法传进的Version值不同时触发
		Log.e("Potato", "onUpgrade");
	}

}

其中,2参数与3参数的构造方法是我自己写的,为了方便调用。

在OnCreate中(也就是第一个创建时,一般新建一张表,系统自动识别,有则不创建,无则创建);


MainActivity.java

package com.example.testsqlite;

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.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button buttonCreate;
	private Button buttonInsert;
	private Button buttonDelete;
	private Button buttonUpdate;
	private Button buttonQuery;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		buttonCreate=(Button)findViewById(R.id.create);
		buttonInsert=(Button)findViewById(R.id.insert);
		buttonDelete=(Button)findViewById(R.id.delete);
		buttonUpdate=(Button)findViewById(R.id.update);
		buttonQuery=(Button)findViewById(R.id.query);
		
		ButtonListener buttonListener=new ButtonListener();
		buttonCreate.setOnClickListener(buttonListener);
		buttonInsert.setOnClickListener(buttonListener);
		buttonDelete.setOnClickListener(buttonListener);
		buttonUpdate.setOnClickListener(buttonListener);
		buttonQuery.setOnClickListener(buttonListener);

	}
	
	class ButtonListener implements OnClickListener{	
		@Override
		public void onClick(View view) {
			if(view.getId()==R.id.create){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字				
				SQLiteDatabase dbReader=databaseHelper.getReadableDatabase();
				Log.e("Potato", "创建成功!");
				dbReader.close();
			}
			if(view.getId()==R.id.insert){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				ContentValues values=new ContentValues();
				values.put("id", 1);
				values.put("name", "potato");
				dbWriter.insert("user", null, values);//表名,null,值对象
				Log.e("Potato", "插入成功!");
				dbWriter.close();		
			}
			if(view.getId()==R.id.delete){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				 dbWriter.delete("user", "id=?", new String[]{"1"});
				 Log.e("Potato", "删除成功!");
				 dbWriter.close();		
			}
			if(view.getId()==R.id.update){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字
				SQLiteDatabase dbWriter=databaseHelper.getWritableDatabase();
				ContentValues newValue=new ContentValues();
				newValue.put("name", "tomato");
				dbWriter.update("user", newValue, "id=?", new String[]{"1"});
				 Log.e("Potato", "更改成功!");
			}
			if(view.getId()==R.id.query){
				DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this, "db_potato"); //第二个参数是数据库的名字				
				SQLiteDatabase dbReader=databaseHelper.getReadableDatabase();
				Cursor cursor=dbReader.rawQuery("select * from user where id=?", new String[]{"1"});
				while(cursor.moveToNext()){
					Log.e("Potato", cursor.getString(cursor.getColumnIndex("name")));
				}
				dbReader.close();
			}	
		}
	}
}





activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/create"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="create"/>

    <Button
        android:id="@+id/insert"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="insert" />

    <Button
        android:id="@+id/delete"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="delete" />

    <Button
        android:id="@+id/update"
       android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="update" />

    <Button
        android:id="@+id/query"
        android:layout_width="244dp"
        android:layout_height="wrap_content"
        android:text="query" />

</LinearLayout>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。