阅读更多 使用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);}/** * 关闭数据库 */@Overridepublic synchronized void close() {if (myDataBase != null)myDataBase.close();super.close();}@Overridepublic void onCreate(SQLiteDatabase db) {}@Overridepublic 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() });}}

更多相关文章

  1. android中滑动SQLite数据库分页加载
  2. Android(安卓)Day02
  3. Android(安卓)通过WebView 调用Js sqlite数据库
  4. android imageswitcher gallery 根据数据库内图片名字进行查看/
  5. android sqlcipher 速度太慢,正确使用sqlcipher for Android
  6. Android使用SQlite数据库
  7. android mediaStore
  8. Android(安卓)内存数据库
  9. Android内部数据库

随机推荐

  1. mysql oracle和sqlserver分页查询实例解
  2. mysql中的limit用法有哪些(推荐)
  3. MySQL删除有外键约束的表数据方法介绍
  4. mysql 5.7.19 二进制最新安装
  5. mysql5.7.19 zip 详细安装过程和配置
  6. mysql zip archive 版本(5.7.19)安装教程
  7. mysql5.7.19 解压版安装教程详解(附送纯净
  8. MySQL 声明变量及存储过程分析
  9. MySQL删除表数据的方法
  10. MySQL预编译功能详解