android SQLite操作

阅读更多
使用Android自带的数据库SQLite,包括连接、复制、增删改查等操作。
直接看代码(直接可以运行的)
package com.named;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
	private static String DB_PATH = "/data/data/com.named/databases/";
	private static String DB_NAME = "mydata.db";
	private static String TB_NAME = "myname";
	private SQLiteDatabase myDataBase;
	private final Context myContext;

	public DataBaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}

	/**
	 * 创建新的数据库
	 * */
	public void createDataBase() throws IOException {
		boolean dbExist = checkDataBase();
		if (dbExist) {
		} else {
			this.getReadableDatabase();
			try {
				copyDataBase();
			} catch (IOException e) {
				throw new Error("Error copying database");
			}
		}
	}

	/**
	 * 检查目录是否有数据库
	 * 
	 * @return
	 */
	private boolean checkDataBase() {
		SQLiteDatabase checkDB = null;
		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READONLY);
		} catch (SQLiteException e) {
			// database does't exist yet.
		}
		if (checkDB != null) {
			checkDB.close();
		}
		return checkDB != null ? true : false;
	}

	/**
	 * 如果没有数据库,就把assets下面的数据库文件拷贝到sd卡data目录下
	 **/
	private void copyDataBase() throws IOException {
		InputStream myInput = myContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	/**
	 * 打开数据库
	 * 
	 * @throws SQLException
	 */
	public void openDataBase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READONLY);
	}

	/**
	 * 关闭数据库
	 */
	@Override
	public synchronized void close() {

		if (myDataBase != null)
			myDataBase.close();
		super.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	/**
	 * 查询表数据
	 * 
	 * @return
	 */
	public String query() {
		String name = "";
		Cursor cursor = myDataBase.query(TB_NAME, new String[] { "name" }, "id"
				+ " = " + 1, null, null, null, null);
		while (cursor.moveToNext()) {
			name = cursor.getString(cursor.getColumnIndex("name"));
		}

		return name;

	}

	/*
	 * 增加
	 */
	public void save() {
		myDataBase.execSQL("insert into person(name) values (?)",
				new Object[] { "tom" });
	}

	/*
	 * 更新
	 */
	public void update() {
		myDataBase.execSQL("update person set name=? where personid=?",
				new Object[] { "kitty", "1" });
	}

	/*
	 * 删除
	 */
	public void delete(Integer id) {
		myDataBase.execSQL("delete from person where personid=?",
				new Object[] { id.toString() });
	}

}

你可能感兴趣的:(android,sqlite,增加,删除,查询)