我已经阅读并尝试在线实现几个示例,但是我发现它们总是缺少一些代码,也不会编辑或者像广告一样工作.
有没有人有一个完整的Android数据库助手类在AndroID上部署现有的sqlite数据库?
这是我想出来的,希望能帮助那些有麻烦的人.package com.MyPackage;import java.io.fileOutputStream;import java.io.IOException;import java.io.inputStream;import java.io.OutputStream;import java.util.UUID;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteException;import androID.database.sqlite.sqliteOpenHelper;import androID.util.Log;public class AnyDBAdapter { private static final String TAG = "AnyDBAdapter"; private DatabaseHelper mDbHelper; private static sqliteDatabase mDb; //make sure this matches the //package com.MyPackage; //at the top of this file private static String DB_PATH = "/data/data/com.MyPackage/databases/"; //make sure this matches your database name in your assets folder // my database file does not have an extension on it // if yours does // add the extention private static final String DATABASE_name = "data"; //Im using an sqlite3 database,I have no clue if this makes a difference or not private static final int DATABASE_VERSION = 3; private final Context adapterContext; public AnyDBAdapter(Context context) { this.adapterContext = context; } public AnyDBAdapter open() throws sqlException { mDbHelper = new DatabaseHelper(adapterContext); try { mDbHelper.createDataBase(); } catch (IOException ioe) { throw new Error("Unable to create database"); } try { mDbHelper.openDataBase(); } catch (sqlException sqle) { throw sqle; } return this; } //Usage from outsIDe // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map // dba.open(); // Cursor c = dba.ExampleSelect("Rawr!"); // contextObject.startManagingCursor(c); // String s1 = "",s2 = ""; // if(c.movetoFirst()) // do { // s1 = c.getString(0); // s2 = c.getString(1); // } while (c.movetoNext()); // dba.close(); public Cursor ExampleSelect(string myVariable) { String query = "SELECT locale,? FROM androID_Metadata"; return mDb.rawquery(query,new String[]{myVariable}); } //Usage // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT); // dba.open(); // dba.ExampleCommand("en-CA","en-GB"); // dba.close(); public voID ExampleCommand(String myVariable1,String myVariable2) { String command = "INSERT INTO androID_Metadata (locale) SELECT ? UNION ALL SELECT ?"; mDb.execsql(command,new String[]{ myVariable1,myVariable2}); } public voID close() { mDbHelper.close(); } private static class DatabaseHelper extends sqliteOpenHelper { Context helperContext; DatabaseHelper(Context context) { super(context,DATABASE_name,null,DATABASE_VERSION); helperContext = context; } @OverrIDe public voID onCreate(sqliteDatabase db) { } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { Log.w(TAG,"Upgrading database!!!!!"); //db.execsql(""); onCreate(db); } public voID createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (dbExist) { } else { //make sure your database has this table already created in it //this does not actually work here /* * db.execsql("CREATE table IF NOT EXISTS \"androID_Metadata\" (\"locale\" TEXT DEFAulT 'en_US')" * ); * db.execsql("INSERT INTO \"androID_Metadata\" VALUES ('en_US')" * ); */ this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } public sqliteDatabase getDatabase() { String myPath = DB_PATH + DATABASE_name; return sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_Readonly); } private boolean checkDataBase() { sqliteDatabase checkDB = null; try { String myPath = DB_PATH + DATABASE_name; checkDB = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_Readonly); } catch (sqliteException e) { } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } private voID copyDataBase() throws IOException { // Open your local db as the input stream inputStream myinput = helperContext.getAssets().open(DATABASE_name); // Path to the just created empty db String outfilename = DB_PATH + DATABASE_name; // Open the empty db as the output stream OutputStream myOutput = new fileOutputStream(outfilename); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myOutput.write(buffer,length); } // Close the streams myOutput.flush(); myOutput.close(); myinput.close(); } public voID openDataBase() throws sqlException { // Open the database String myPath = DB_PATH + DATABASE_name; mDb = sqliteDatabase.openDatabase(myPath,sqliteDatabase.OPEN_READWRITE); } @OverrIDe public synchronized voID close() { if (mDb != null) mDb.close(); super.close(); } }}总结
以上是内存溢出为你收集整理的什么是现有SQLite数据库的完整Android数据库帮助器类?全部内容,希望文章能够帮你解决什么是现有SQLite数据库的完整Android数据库帮助器类?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)