0

我尝试从表产品中选择数据,但是发现“android.database.sqlite.SQLiteException: no such table: product (code 1):, while compile: SELECT * FROM product”错误

public Cursor getproduct(){
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cur = null;
    try{
        cur=db.rawQuery("SELECT  * FROM product",new String [] {});
        cur.moveToFirst();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return cur;
}
4

3 回答 3

0

您在查询中编写的表名可能有一些错误,表名可能拼写错误。

于 2013-10-18T09:28:11.627 回答
0

我建议对例如表名使用静态引用,以免在此犯任何错误。你的表的 onCreate() 方法(关于其他人所指的)应该看起来像这样:

public class DbHelper extends SQLiteOpenHelper 
{
private static String DATABASE_NAME = "FodoSubstitutes.db";
private static String FOOD_TABLE = "Food";

//Creates the database with db name and calls onCreate(). 
public DbHelper(Context context) 
{
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) 
{
    //System.out.println("in onCreate");
    //assocID   food    healthierFood category  description count submittedBy
    String sql = "CREATE TABLE IF NOT EXISTS " + FOOD_TABLE +
                "(Food_ID integer primary key, " + 
                "Food_Name string not null, " +
                "Food_Aliases string, " + 
                "Hints string, " +
                "Category string, " + 
                "Subcategory string, " +
                "Description string, " + 
                "Submission_ID int, " +
                "Comment_ID int, " + 
                "Count int); ";
   db.execSQL(sql);

}
}
于 2013-10-18T07:42:53.447 回答
0

问题是 getReadableDatabase() 方法没有返回您想要的数据库。您需要在应用程序中构建它或将其与应用程序捆绑在一起(使用资产)。以下是执行这两项操作的代码片段:

建立数据库

    static final string DATABASE_CREATE = "create table if not exists " + "product " + "(" +
    TABLE_COLUMN_1 + " " + COLUMN_1_TYPE + ", " + 
    TABLE_COLUMN_2 + " " + COLUMN_2_TYPE + ", "; //and so on
    public class MySQLHelper extends SQLiteOpenHelper {
    MySQLHelper(Context context) {
       super(context, YOUR_DATABASE_NAME, null, YOUR_DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
       try {
           db.execSQL(DATABASE_CREATE);
       }
       catch(SQLException e) {
           e.printStackTrace();
       }
    }

使用资产复制预建数据库

首先,您需要将 SQL 文件放在应用程序的 assets 文件夹中。然后将此代码放在主要活动的 onCreate() 方法中:

File f = new File(this.getDatabasePath().getAbsolutePath() + "/mydatabase.db";
if(!f.exists()) {
   if(f.getParentFile().exists() || f.getParentFile().mkdirs()) {
       f.createNewFile();
       InputStream inputStream = getBaseContext().getAssets().open("mydatabase.db");
       OutputStream outputStream = new FileOutputStream(f);
       byte[] buffer = new byte[1024];
       int length;
       while((length = dbStream.read(buffer)) > 0) {
           outputStream.write(buffer, 0, length);
       }
       inputStream.close();
       outputStream.flush();
       outputStraem.close();
    }
    else
       throw new IOException("Database input error");
    }
}

这是使用预建数据库放置在“mydatabase.db”文件中的假设。

于 2013-10-18T07:51:34.727 回答