我正在尝试将预先存在的数据库加载到我的Android设备中。
我使用了以下步骤:
SQLiteAssetHelper
然后根据文档:编写一个包装类
public class DataBaseHelper extends SQLiteAssetHelper {
private static final String TAG = "DataBaseHelper";
private static final String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final String TABLE_EXAMPLE = "example"; //example is the table name
private static final String COLUMN_EXAMPLE_A = "A";
public String searchSomething() {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = getReadableDatabase(); //database name
//parameters in query method
String tables = "table"; //
String[] projectionln = {"test"}; //which columns to return, null return all
String selection = "row_name = example"; //which rows to return, formatted as WHERE clause excluding WHERE
String[] selectionArgs = null; //you may include ? in selection which will be filled in by selectionArgs in order
String groupBy = null; //how to group rows
String having = null; //filter which row groups to include in cursor
String sortOrder = null; //how to order the rows
qb.setTables(tables);
Cursor cursor = qb.query(db, projectionln, selection, selectionArgs, groupBy, having, sortOrder);
cursor.moveToFirst();
return cursor.getString(1);
}
}
在Activity
中实例化包装类以使用数据库
喜欢这个
public class MainActivity extends AppCompatActivity {
private DataBaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DataBaseHelper(this);
final String name = db.searchSomething();
}
}
将数据库文件放在main/src/assets/databases
下的项目中,例如,如果数据库文件名为test.db,则文件位于main / src / assets / databases / test.db
Add to your database special meta-information like described here
完成所有这些后,我留下的错误让我相信我的数据库文件格式还不好。我在github上使用了库提供的数据库而且工作正常。
06-16 13:38:56.957 9281-9281/com.mycompany.programname E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.programname, PID: 9281
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.programname/com.mycompany.programname.mainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
at com.mycompany.programname.DataBaseHelper.searchSomething(DataBaseHelper.java:75)
at com.mycompany.programname.HomescreenActivity.onCreate(HomescreenActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
现在我的问题是,如何以正确的sql db
格式获取数据库文件?我认为这一定是问题所在。现在我有一个excel文档,我使用程序DB browser of SQLite中的import函数将其转换为sql格式。然后我保存数据库,然后有sql扩展,我在互联网上搜索如何将其转换为.db,我发现你可以简单地更改扩展名。但我不认为这是真的。希望有人有任何想法或提示,因为我被困在这一天超过一天。
答案 0 :(得分:3)
现在我有一个excel文档,我将其转换为sql格式。
我不知道你考虑什么" sql格式"是。
我在互联网上搜索如何将其转换为.db,我发现您只需更改扩展名即可。这是真的吗?
没有
如果" sql格式"表示"包含一系列SQL语句的纯文本文件",您可以尝试使用任何数量的SQLite工具,例如 sqlite3
程序SQLite本身附带。或者,尝试通过在您最喜爱的搜索引擎上sqlite clients
搜索找到的SQLite客户端。
或者,自己编写一个在您的开发计算机上运行的程序,该程序将您的Excel数据转换为SQLite数据库。例如,{APK 3}中的my book带有一个预先打包的SQLite数据库形式的全文索引,我通过SQLiteAssetHelper
加载。我的书籍发布过程的一部分是使用Ruby脚本和SQLite Ruby gem创建该数据库。