2013-10-18 89 views
0

我尝试从表产品中选择数据,但是“android.database.sqlite.SQLiteException:no such table:product(code 1):,while compiling:SELECT * FROM product”error is found在android中选择查询

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; 
} 
+0

你需要显示你的数据库类的'onCreate()'代码 – Shubhank

+0

只需简单地写''cur = db.rawQuery(“SELECT * FROM product”);''因为你没有指定任何特定的列。 – GrIsHu

+0

你有onCreate在你的productDB类里面? – Shubhank

回答

0

我会建议使用静态引用来eg表名不会在这里产生任何错误。和的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); 

} 
} 
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文件在您的应用程序的资产文件夹中。然后把这个代码的主要活动的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”文件。

0

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