TEH 我正在尝试使用SQLite数据库。不幸的是,在我的onCreate方法上,我的数据库永远不会被创建,崩溃任何人都可以帮助我。 我得到的错误是:没有这样的表:firstdatabase。
这是我的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);
}
答案 0 :(得分:1)
您的CREATE_PROTOTYPE_TABLE
字符串不正确,分号丢失(;):
使用以下内容:
String CREATE_PROTOTYPE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_ISBN + " TEXT,"
+ KEY_INGREDIENTS + " TEXT" + ");";