在android中选择查询

时间:2013-10-18 07:24:13

标签: android sql sqlite

我尝试从表产品中选择数据,但“android.database.sqlite.SQLiteException:no such table:product(code 1):, while while compiling: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;
}

3 个答案:

答案 0 :(得分:0)

我建议使用静态引用来例如表名在这里没有任何错误。和onCreate() - 你的表的方法(至于其他人所指的是什么)shoudl看起来像这样:

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);

}
}

答案 1 :(得分:0)

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

构建数据库

    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”文件中。

答案 2 :(得分:0)

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