我遇到了SQLite数据库的问题。它只是工作正常,但当我添加扩展" .db"使用数据库名称,整个应用程序崩溃。它没有构建任何数据库。我想知道是否有人可以提供帮助!我需要查看数据库但不能使用扩展名。
public class DataBaseHelper extends SQLiteOpenHelper {
private Context mycontext;
private static String DB_NAME = "newsapp.db";
public SQLiteDatabase myDataBase;
private String DB_PATH = "/data/data/"
+ "com.news.mint"
+ "/databases/";
public DataBaseHelper(Context context) throws IOException {
super(context, DB_NAME, null, 1);
this.mycontext = context;
boolean dbexist = checkdatabase();
if (dbexist ) {
opendatabase();
} else {
createdatabase();
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
// SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch (SQLiteException e) {
}
return checkdb;
}
private void copydatabase() throws IOException {
// Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer)) > 0) {
myoutput.write(buffer, 0, length);
}
// Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException {
// Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
super.close();
if (myDataBase != null) {
myDataBase.close();
}
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
LogCat:
09-23 14:34:10.895: E/AndroidRuntime(7477): FATAL EXCEPTION: main
09-23 14:34:10.895: E/AndroidRuntime(7477): android.database.sqlite.SQLiteException: no such table: primary_keywords (code 1): , while compiling: SELECT stock_Symbol from primary_keywords where stock_id = '22628'
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
09-23 14:34:10.895: E/AndroidRuntime(7477): at com.newsmint.databasemanagementsystem.QueryManager.getDataLocal(QueryManager.java:36)
09-23 14:34:10.895: E/AndroidRuntime(7477): at com.newsmint.stockmint.NewsActivityFragment.init(NewsActivityFragment.java:74)
09-23 14:34:10.895: E/AndroidRuntime(7477): at com.newsmint.stockmint.NewsActivityFragment.onResume(NewsActivityFragment.java:147)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.os.Handler.handleCallback(Handler.java:615)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.os.Looper.loop(Looper.java:137)
09-23 14:34:10.895: E/AndroidRuntime(7477): at android.app.ActivityThread.main(ActivityThread.java:4921)
09-23 14:34:10.895: E/AndroidRuntime(7477): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 14:34:10.895: E/AndroidRuntime(7477): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 14:34:10.895: E/AndroidRuntime(7477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
09-23 14:34:10.895: E/AndroidRuntime(7477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
09-23 14:34:10.895: E/AndroidRuntime(7477): at dalvik.system.NativeStart.main(Native Method)
09-23 14:56:47.185: E/SQLiteLog(7870): (1) no such table: top_news
答案 0 :(得分:0)
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +"your table name");
}
答案 1 :(得分:0)
我认为
为了使它工作,你应该做两件事:
将数据库创建逻辑从DataBaseHelper-Constructor
移至
@Override
public SQLiteDatabase openOrCreateDatabase(final String name,
final int mode, final SQLiteDatabase.CursorFactory factory) {
你应该替换
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
带
...
File dbfile = context.getDatabasePath(DB_NAME)
// create directory if it does not exist. Else copydatabase will fail because directory does not exist yet.
if (!dbfile.getParentFile().exists()) {
dbfile.getParentFile().mkdirs();
}
在测试此代码之前,您应手动删除数据库(或卸载应用程序)
/data/data/com.news.mint/databases/newsapp.db
强制执行新的数据库创建
答案 2 :(得分:0)
嗯,它没有用扩展来解决它,但我现在能够使用Questoid sqlitebrowser查看数据库,该数据库适用于没有扩展名的数据库。