我收到了无法找到该表的错误。
我尝试了通过冲浪找到的解决方案,就像在模拟器中擦除数据库一样,但它没有用。
我认为这与从不同的类读取数据库有关,但我不知道如何管理它。
P.S。我目前正在使用Android Studio' SlidingTabsBasic '提供的示例
SlidingTabsBasicFragment.java
public class SlidingTabsBasicFragment extends Fragment {
static final String LOG_TAG = "SlidingTabsBasicFragment";
final String[] genre = {"고전문학", "b", "c", "d", "e","f","g","h","i","j" };
String[] genre1;
final String[] genre2 = {"...","3","4","5"};
SQLiteDatabase db;
MySQLiteOpenHelper helper;
...
class SamplePagerAdapter extends PagerAdapter {
...
public Object instantiateItem(ViewGroup container, int position) {
helper = new MySQLiteOpenHelper(getActivity(), MainActivity.DATABASE_NAME, null, MainActivity.DATABASE_VERSION);
db = helper.getWritableDatabase();
String sql_genre1 = "select name from " + MySQLiteOpenHelper.DATABASE_TABLE_NAME +
"";
Cursor cursor_genre1 = db.rawQuery(sql_genre1,null); // where problem occurs
if (cursor_genre1!=null){
int count_genre1 = cursor_genre1.getCount();
String[] temparray_genre1 = new String[count_genre1+1];
temparray_genre1[0]="...";
for (int i=1; i<=count_genre1 ; i++){
temparray_genre1[i] = cursor_genre1.getString(0);
cursor_genre1.moveToNext();
}
genre1 = temparray_genre1;
}
...
MySQLiteOpenHelper.java
package com.example.android.slidingtabsbasic;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
final static public String DATABASE_TABLE_NAME = "LDB";
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTable(db);
insertData(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists database";
db.execSQL(sql);
onCreate(db);
}
public void createTable(SQLiteDatabase db) {
String sql ="create table " + DATABASE_TABLE_NAME
+ "(name text,"
+ "author text,"
+ "genre text,"
+ "isBookmarked integer)";
db.execSQL(sql);
}
public void insertData(SQLiteDatabase db) {
db.beginTransaction();
insertintoLDB(db,"공무도하가", "작자미상", "고대가요");
insertintoLDB(db,"구지가","작자미상","고대가요");
insertintoLDB(db,"해가","작자미상","고대가요");
insertintoLDB(db,"정읍사","작자미상","고대가요");
insertintoLDB(db,"황조가","유리왕","고대가요");
db.endTransaction();
}
public void insertintoLDB(SQLiteDatabase db, String name, String author, String genre){
String sql = "insert into " + DATABASE_TABLE_NAME + "(name, author, genre, isBookmarked) "
+"values('"+ name + "','" + author + "','" + genre + "',0)";
db.execSQL(sql);
}
}
MainActivity.java
public class MainActivity extends SampleActivityBase {
public static final String TAG = "MainActivity";
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "LiteratureDatabase.db";
SQLiteDatabase db;
MySQLiteOpenHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDatabase();
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
}
public void createDatabase() {
helper = new MySQLiteOpenHelper(this, DATABASE_NAME, null, DATABASE_VERSION);
db = helper.getWritableDatabase();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
logcat的
07-23 08:50:24.075 27205-27205/com.example.android.slidingtabsbasic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.slidingtabsbasic, PID: 27205
android.database.sqlite.SQLiteException: no such table: LDB (code 1): , while compiling: select name from LDB
答案 0 :(得分:2)
您的设备上有一个旧版本的数据库,它有(空)数据库,但没有books表。如果这是您的选项,只需卸载并重新安装该应用程序。