由于我在Android应用程序中处理许多表,我决定在SQLite管理器中创建数据库(DB)(如firefox插件或用于SQLite的navicat)。我完成了创建它们并导出.sql文件!但是我不知道如何使用这个文件!当然,我找到了一些关于这个的帖子,把DB(在一个根手机上)放在。。。data/data/{您的包名称}/。。等等,但我不明白这一步之后该怎么办!!(我只知道将数据库引入android应用程序的代码!!)
-
有人能一步一步告诉我如何在我的应用程序中使用这些方法来使用我创建的数据库吗?-
-
这是使用这些SQLite管理器的好方法吗?还是尽管在android中用代码创建多表很复杂,但你更喜欢它???
-
sqlite 2和3之间的主要区别是什么?它们是由不同的Android版本支持,还是仅由Sqlite的更新支持?
编辑:将dB复制到assest文件夹并使用复制代码后:
03-16 11:32:2.0370:I/SqliteDatabaseCpp(3999):sqlite返回:错误代码=14,msg=无法打开[8609a15dfa]第27712行的文件,db=/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite03-16 11:32:20370:I/SqliteDatabaseCpp(3999):sqlite返回:错误代码=14,msg=os_unix.c:open(),第27712行-"errno=2 path=/data/data/com.androidhive.xmlparsing/databases/surveydb.sqlite,db=/data/doata/com.ndroidhive.xml parsing/databases/survedb.sqlite03-16 11:32:2.0370:E/SqliteDatabaseCpp(3999):sqlite3_open_v2("/data/data.com/androidhive.xmlparsing/databases/ssurveydb.sqlite",&handle,1,NULL)失败03-16 11:32:20:80:E/SQLiteDatabase(3999):打开数据库失败。关闭它。03-16 11:32:20:80:E/SQLiteDatabase(3999):android.database.sqlite.SQLiteCantOpenDatabase异常:无法打开数据库文件03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.database.sqlite.SQLiteDatabase.dbgopen(Native Method)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:932)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在数据库。AssetDatabaseHelper.checkExist(AssetDatabaseHelpr.java:55)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在数据库。AssetDatabaseHelper.importIfNotExist(AssetDatabaseHelpr.java:81)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParssingActivity.java:78)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:1782)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:1834)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.ActivityThread.access$500(ActivityThreads.java:122)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1027)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.os.Handler.dispatchMessage(Handler.java:99)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.os.Looper.loop(Looper.java:132)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在android.app.ActivityThread.main(ActivityThreads.java:4126)03-16 11:32:2.0380:E/SQLiteDatabase(3999):位于java.lang.reflect.Method.invokeNative(Native Method)03-16 11:32:2.0380:E/SQLiteDatabase(3999):位于java.lang.reflect.Method.ioke(Method.java:491)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在com.android.internal.os.ZygoteInit.main(ZygoteIndit.java:602)03-16 11:32:2.0380:E/SQLiteDatabase(3999):在dalvik.system.NativeStart.main(Native方法)03-16 11:32:20.380:W/System.err(3999):android.database.sqlite.SQLiteCantOpenDatabase异常:无法打开数据库文件03-16 11:32:20.380:W/System.err(3999):在android.database.sqlite.SQLiteDatabase.dbgopen(本机方法)03-16 11:32:20.380:W/System.err(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)03-16 11:32:20.380:W/System.err(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)03-16 11:32:20.380:W/System.err(3999):在android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:932)03-16 11:32:20.380:W/System.err(3999):在数据库中。AssetDatabaseHelper.checkExist(AssetDatabaseHelpr.java:55)03-16 11:32:20.380:W/System.err(3999):在数据库中。AssetDatabaseHelper.importIfNotExist(AssetDatabaseHelpr.java:81)03-16 11:32:20:80:W/System.err(3999):在com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParssingActivity.java:78)03-16 11:32:2.0380:W/System.err(3999):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)03-16 11:32:20.380:W/System.err(3999):在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:1782)03-16 11:32:20.380:W/System.err(3999):在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:1834)03-16 11:32:2.0380:W/System.err(3999):在android.app.ActivityThread.access$500(ActivityThreads.java:122)03-16 11:32:20.380:W/System.err(3999):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)03-16 11:32:20.380:W/System.err(3999):在android.os.Handler.dispatchMessage(Handler.java:99)03-16 11:32:20.380:W/System.err(3999):在android.os.Looper.loop(Looper.java:132)03-16 11:32:20.380:W/System.err(3999):在android.app.ActivityThread.main(ActivityThreads.java:4126)03-16 11:32:20.380:W/System.err(3999):位于java.lang.reflect.Method.invokeNative(Native Method)03-16 11:32:2.0380:W/System.err(3999):在java.lang.reflect.Method.ioke(Method.java:491)03-16 11:32:2.0380:W/System.err(3999):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)03-16 11:32:2.0380:W/System.err(3999):在com.android.internal.os.ZygoteInit.main(ZygoteNit.java:602)03-16 11:32:20.380:W/System.err(3999):在dalvik.System.NativeStart.main(Native Method)
我使用了这个代码:
public class AssetDatabaseHelper extends SQLiteOpenHelper{
private String dbName;
private String db_path;
private Context context;
/**
* A helpe r class to import db files.
*
* @param context
* base/app context
* @param dbName
* The name of the db in asset folder .
*/
public AssetDatabaseHelper(Context context, String dbName) {
super(context, dbName, null, 1);
this.dbName = dbName;
this.context = context;
db_path = "/data/data/" + context.getPackageName() + "/databases/";
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
public boolean checkExist() {
SQLiteDatabase checkDB = null;
try {
String myPath = db_path + dbName;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
} catch (Exception ep) {
ep.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void importIfNotExist() throws IOException {
boolean dbExist = checkExist();
if (dbExist) {
// do nothing - database already exist
} else {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Creates copies the database from asset to the new database location.
* */
private void copyDatabase() throws IOException {
InputStream is = context.getAssets().open(dbName);
OutputStream os = new FileOutputStream(db_path + dbName);
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
this.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
更新日期2:db图片:
CREATE TABLE"**"("_id"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"**"TEXT,"***"整数);CREATE TABLE"***"("_id"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"***"INTEGER UNIQUE,"****"TEXT);CREATE TABLE"**";CREATE TABLE"**"("_id"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"**"TEXT);CREATE TABLE"**";CREATE TABLE"*"("_id"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"**"INTEGOR NOT NULL UNIQUE,"***"INTEGERA,"**"INTEGER,"**"INTGER,"***"TEXT,"**"TEXT);CREATE TABLE"**"("_id"整数主键,"***"文本);CREATE TABLE"**"("_id"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"**"TEXT,"srDesc"TEXT"***"INTEGER","***"DATETIME"**"DATETIME,"***"整数,"srCode"整数);CREATE TABLE"android_metadata"("locale"TEXT DEFAULT'en_US')
最后:
new AssetDatabaseHelper(this , "surveydb.sqlite").importIfNotExist();
问题出在哪里?
你能帮我吗?
- Sqlite 3在Android中受支持
- 使用导入的数据库不需要根手机
使用导入数据库的步骤:
- 将数据库转储到/assets文件夹中
- 在此处添加github中的代码
-
通过执行下面的代码导入数据库。
AssetDatabaseHelper dbHelper = new AssetDatabaseHelper( getBaseContext(), YOUR_DATABASE_NAME); try { dbHelper.importIfNotExist(); } catch (IOException e) { e.printStackTrace(); }