阅读更多 使用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操作数据的动态更新ListView
  3. Android 文件操作大全
  4. android xliff字符串操作
  5. Android 通过WebView 调用Js sqlite数据库
  6. android imageswitcher gallery 根据数据库内图片名字进行查看/
  7. Android使用SQlite数据库
  8. android中的短信操作

随机推荐

  1. Volley String cannot be converted to J
  2. textView 密码问题
  3. android intent使用总结
  4. android中DisplayMetrics类的作用
  5. Java(Android)数据结构汇总(二)-- Set(上)
  6. android开源项目参考
  7. Android中px、dp和sp区别
  8. ndroid显示在线图片
  9. Android线程间通信Handler机制(Android开
  10. 解决:Failed to fetch URL https://dl-ssl