2016-07-08 99 views
-1

teH 我想开始使用SQLite数据库。不幸的是,在我的onCreate方法中,我的数据库永远不会被创建,任何人都可以帮助我解决问题。 我得到的错误是:没有这样的表:firstdatabase。SQLIte不创建数据库表

这是我MySQLiteHelper类:

public class MySQLiteHelper extends SQLiteOpenHelper { 


    //Database instance 
    //private static MySQLiteHelper sInstance; 

    //Database name 

    private static final String DATABASE_NAME = "TestDB"; 

    //Table name 
    private static final String TABLE_NAME = "firstdatabase"; 
    //column names of the table 
    private static final String KEY_ID = "id"; 
    private static final String KEY_ISBN = "isbn"; 
    private static final String KEY_INGREDIENTS = "ingredients"; 

    //Database Version 
    private static final int DATABASE_VERSION = 2; 

    // Log TAG for debugging purpose 
    private static final String TAG = "SQLiteAppLog"; 

    //Method that will ensure only one instance of the database is created. 
    /* public static synchronized MySQLiteHelper getsInstance(Context context){ 
     if(sInstance == null){ 
      sInstance = new MySQLiteHelper(context.getApplicationContext()); 
     } 
     return sInstance; 
    } 
    */ 

    // Constructor 
    public MySQLiteHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     Log.d(TAG, "Inside SQLITEHELPER METHOD()"); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     //SQLite statement to create a table called 'TestDB'. 
     String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ISBN + " TEXT," 
       + KEY_INGREDIENTS + " TEXT" + ")"; 

     Log.d(TAG, "DB created"); 
     db.execSQL(CREATE_PROTOTYPE_TABLE); 
     //Log.d(TAG, "DB created"); 

    } 

    // onUpdate() is invoked when you upgrade the database scheme. 
    // Don’t consider it seriously for the sample app. 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older prototype table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 

     this.onCreate(db); 
    } 

    public void addIngredientsToTable(Product product){ 

     Log.d(TAG, "adding ingredients to table"); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_ISBN, product.getIsbn()); 
     values.put(KEY_ISBN, product.getIngredients()); 

     db.insert(TABLE_NAME,null, values); 
     db.close(); 
    } 

    //Method to get the ingredients list based on the isbn passed in. 
    public ArrayList<String> getIngredients(String isbn){ 
     ArrayList<String> ingredientList = new ArrayList<String>(); 
     String isbnPassedIn = isbn; 

     String query = "SELECT isbn, ingredients FROM " + TABLE_NAME + " WHERE isbn = " + isbnPassedIn; 

     //Getting instance to a readable database 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 

     if(cursor.moveToFirst()){ 
      do{ 
       ingredientList.add(cursor.getString(1)); 
       ingredientList.add(cursor.getString(2)); 
      }while (cursor.moveToFirst()); 

     } 
     Log.d(TAG, "Inside getIngredients()"); 
     return ingredientList; 
    } 


    //Method to get all the list of products in the datbase. 
    public ArrayList<Product> getAllProducts(){ 
     ArrayList<Product> productList = new ArrayList<Product>(); 

     String selectQuery = "SELECT * FROM firstdatabase"; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery,null); 

     if(cursor.moveToFirst()){ 
      do{ 
       Product product = new Product(); 
       product.setId(Integer.parseInt(cursor.getString(0))); 
       product.setIsbn(cursor.getString(1)); 
       product.setIngredients(cursor.getString(2)); 
       productList.add(product); 
      }while (cursor.moveToNext()); 
     } 
     return productList; 

    } 

的Java文件:

public class BarCodeActivity extends AppCompatActivity { 

    MySQLiteHelper db1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_bar_code); 
     db1 = new MySQLiteHelper(this); 
     Log.d("Insert", "Inserting data..."); 
     db1.addIngredientsToTable(new Product(1, "89470001033", "Lowfat yogurt, mango, milk, water")); 

     Log.d("Reading: ", "Reading all products"); 
     ArrayList<Product> products = db1.getAllProducts(); 

     for(Product product : products){ 
      String log = "Id: " + product.getId() + ", ISBN: " + product.getIsbn() + ", Ingredients: " + product.getIngredients(); 
      Log.d("Product:", log); 
     } 
+2

你复制了错误的文件吗?它是同一个文件的两倍。 'MySQLiteHelper'缺失。 – dipdipdip

+1

是的,请张贴正确的文件,以及错误** logcat **。 – Vucko

+0

可能的重复http://stackoverflow.com/questions/6554269/android-sqlite-no-such-table-error –

回答

1

CREATE_PROTOTYPE_TABLE字符串是不正确的,分号丢失(;):

使用以下命令:

String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_ISBN + " TEXT," 
       + KEY_INGREDIENTS + " TEXT" + ");"; 
+0

如果它为你工作,然后请投票我的答案。 – Saini