2013-10-18 88 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

您在查詢中寫入的表名可能存在一些錯誤,表名可能拼寫錯誤。