【问题标题】:Android Database - No such table errorAndroid 数据库 - 没有这样的表错误
【发布时间】:2014-01-05 20:44:41
【问题描述】:

我正在尝试使用几个 http 请求来填充数据库中的几个表和 android 应用程序。当这些表格被填充时,表格中的部分数据将填充并展开视图。问题是我收到一条错误消息,告诉我我正在尝试创建的表尚未创建。

错误日志:

01-05 13:33:29.811: E/SQLiteLog(1823): (1) no such table: lvl2List

01-05 13:33:29.831: E/AndroidRuntime(1823): android.database.sqlite.SQLiteException: no such table: lvl2List (code 1): , while compiling: SELECT id, Title, Description FROM lvl2List

这是我的代码:

MainActivity.java

public class MainActivity extends Activity 
{
    private List<lvItem> myLVItems = new ArrayList<lvItem>();
    private List<Parent> myParent = new ArrayList<Parent>();
    ExpandableListView exv;
    final DBHelper db2 = new DBHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        exv=(ExpandableListView)findViewById(R.id.expandableListView1);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db2.open();

        lvl3 lvl3Item = new lvl3();
        lvl3Item.popDB(db2, myParent);
    }
}

DBHelper.java

public class DBHelper 
{
    private static final String Database_Name = "MyDBs";
    private static final int Database_Version = 2;

    public static final String Key_ID = "id";
    public static final String Key_Title = "Title";
    public static final String Key_Summary = "Summary";
    public static final String Key_Book = "Book";
    public static final String Key_Chapter = "Chapter";
    public static final String Key_Description = "Description";
    public static final String Key_SourceType = "SourceType";

    private static final String Lvl3_Table_Name = "lvl3";
    private static final String Lvl2_Table_Name = "lvl2List";
    private static final String Lvl1_Table_Name = "lvl1List";

    private static final String Lvl3_Create = "CREATE TABLE IF NOT EXISTS lvl3 (id integer primary key autoincrement, "
        +"Summary VARCHAR, Book VARCHAR, Chapter VARCHAR, Description VARCHAR, SourceType VARCHAR);";
    private static final String Lvl2_Create = "CREATE TABLE IF NOT EXISTS lvl2List (id integer primary key autoincrement, "
        +"Title VARCHAR, Description VARCHAR);";
    private static final String Lvl1_Create = "CREATE TABLE IF NOT EXISTS lvl1List (id integer primary key autoincrement, "
        +"Title VARCHAR, Description VARCHAR);";

    public DBHelper(Context ctx) {
        this.context = ctx;
        DBhelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, Database_Name, null, Database_Version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            try
            {
                db.execSQL(Lvl3_Create);
                db.execSQL(Lvl2_Create);
                db.execSQL(Lvl1_Create);
            }catch(SQLException e){

            }

        }

        public long insertLvl2Record(String Title, String Description)
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(Key_Title, Title); 
            initialValues.put(Key_Description, Description); 

            return db.insert(Lvl2_Table_Name, null, initialValues);
        }

        public long insertLvl3Record(String Summary, String Book, String Chapter, String Description, String SourceType)
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(Key_Summary, Summary); 
            initialValues.put(Key_Book, Book); 
            initialValues.put(Key_Chapter, Chapter); 
            initialValues.put(Key_Description, Description); 
            initialValues.put(Key_SourceType, SourceType); 

            return db.insert(Lvl3_Table_Name, null, initialValues);
        }

        public int getLvl2Count()
        {
            Cursor c = db.query(Lvl2_Table_Name, new String[] {Key_ID, Key_Title,Key_Description}, 
            null, null, null, null, null);

            return c.getCount();
        }

        public int getLvl3Count()
        {
            Cursor c = db.query(Lvl3_Table_Name, new String[] {Key_ID, Key_Summary,Key_Book,Key_Verse,Key_Scripts,Key_Description,Key_SourceType}, null, null, null, null, null);

            return c.getCount();
        }

lvl3.java

public class lvl3
{
    DBHelper db2;
    List<Parent> parent;
    public void popDB(DBHelper db, List<Parent> myParent) 
    {
        this.db2 = db;
        this.parent = myParent;
        Lvl3Thread lvl3theard = new Lvl3Thread();
        lvl3theard.execute();
    }
    public class Lvl3Thread extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... params) 
        {
            //Httprequest that get the data for the database… this works
        }
        @Override
        protected void onPostExecute(String result) 
        {
            lvl2 lvl2Item = new lvl2();
            lvl2Item.popDB(db2, parent);
        }
    }
}

lvl2.java

public class lvl3
{
    DBHelper db2;
    List<Parent> parent;
    public void popDB(DBHelper db, List<Parent> myParent) 
    {
        this.db2 = db;
        this.parent = myParent;
        Lvl3Thread lvl3theard = new Lvl3Thread();
        lvl3theard.execute();
    }
    public class Lvl3Thread extends AsyncTask<String, Integer, String>{
        @Override
        protected String doInBackground(String... params) 
        {
            //Httprequest that get the data for the database… this works
        }
        @Override
        protected void onPostExecute(String result) 
        {
            lvl2 lvl2Item = new lvl2();
            lvl2Item.popDB(db2, parent);
        }
    }
    public void popLvl2List(DBHelper db)
    {
        List<lvItem> childlvItems = new ArrayList<lvItem>();
        Cursor cLvl3 = db.getAllLvl3Records();
        db.getLvl2Count(); //This is where the error is coming from stating that there is no lvl2List table
    }
}

我尝试过使用db2.detLvl2Count();,但这不起作用。如果我注释该行 out the program will run. I have tried reordering thedb.execSQLso that db.execSQL(Lvl2_Create); 是第一行,但我仍然收到表不存在的错误。

【问题讨论】:

  • 你为什么使用Key_ID, Key_Title,Key_Description,列名是id、Title和Description。
  • 你使用的是什么底层数据库?通常,varchar 作为一种类型需要最大长度(类似于varchar(255))。
  • @RohanKandwal 我编辑了 DBHelper.java 以阐明 Key_ID、Key_Title、Key_Description... 谢谢
  • @GordonLinoff 我正在使用 SQLiteDatabase。所有 VarChar 都将低于 255
  • @user908759 onCreate 您的DBHelper 类只会在您第一次打开应用程序并调用该类时被调用。您后来有没有偶然添加lvl2List 表?如果是这样,请尝试重新安装该应用程序并重试。添加/更改 SQL 表时,使用 onUpgrade 方法:developer.android.com/reference/android/database/sqlite/…, int, int) 同样在 DBHelperonCreate 方法中,您正在“吃掉”SQLException - 不要这样做 -它可能会为您提供有趣的信息;-)

标签: android sql httprequest


【解决方案1】:

您的DBHelper 类的onCreate 只会在您第一次打开应用程序并调用该类时被调用。

您后来有没有偶然添加lvl2List 表?

如果是这样,请尝试重新安装应用程序并重试。

添加/更改 SQL 表时,使用onUpgrade 方法。见official documentation

另外,在您的 DBHelperonCreate 方法中,您正在“吃掉”SQLException - 不要这样做 - 它可能会为您提供有趣的信息 ;-)

通过吃Exception 我的意思是,你“默默地”处理异常并继续前进。

这是DatabaseHelperonCreate 方法:

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(Lvl3_Create);
        db.execSQL(Lvl2_Create);
        db.execSQL(Lvl1_Create);
    } catch (SQLException e) {
        // Handle a possible exception here.
    }
}

注意catch 子句。在这里你至少应该做e.printStacktrace();Log.e(e);

在你的情况下,你可能没有从这个日志中得到任何东西,因为 onCreate 方法只在你第一次安装和启动应用程序时被调用。

看到这个SO post关于这个问题或者只是搜索Google - 这是我们作为程序员倾向于做的一个已知的“问题”/“行为”;-)

关于如何处理异常的一个很好的线索,可以找到here

【讨论】:

    【解决方案2】:

    我知道这个话题已经过时并且已经解决了。但我想为像我这样有同样问题的其他人提供这个提示。尝试在文件名中包含 .db,例如:

        private static final String Database_Name = "MyDBs.db";
    

    这解决了我的问题

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      相关资源
      最近更新 更多