What is wrong with the following peace of code for creating a table in Android-:
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT "
+ COLUMN_PRODUCTNMAE + " TEXT " + ");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
// add a new row to teh database
public void addProduct(Products product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNMAE, product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(DATABASE_NAME, null, values);
db.close();
}
// delete the product fromt he database
public void deleteProduct(String productname) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNMAE + " =\"" + productname + "\";");
}
// Print out hte database
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
// CURSOR POINTS TO A LOCATION OF YOUR RESULTS
Cursor c = db.rawQuery(query, null);
// move to the first row in the result
c.moveToFirst();
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("productname")) != null) {
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
}
return dbString;
}
The error is coming while inserting that it is unable to insert any element into the table as the table doesn't exist. so can Somebody tell me what is wrong with above code for creating table. The error log is-:
10-29 19:30:53.742: E/AudioService(720): handleConfigurationChanged() createInstance IAudioServiceExt fail
10-29 19:30:55.277: E/RemoteViews(720): ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
10-29 19:30:55.277: E/RemoteViews(720): ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
10-29 19:30:59.395: E/SQLiteLog(11765): (1) no such table: products.db
10-29 19:30:59.402: E/SQLiteDatabase(11765): Error inserting productname=hari
10-29 19:30:59.402: E/SQLiteDatabase(11765): android.database.sqlite.SQLiteException: no such table: products.db (code 1): , while compiling: INSERT INTO products.db(productname) VALUES (?)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at andrid.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1492)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1364)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at com.example.pranav.savedata.myDbHandler.addProduct(myDbHandler.java:50)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at com.example.pranav.savedata.gopal.add(gopal.java:38)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.view.View.performClick(View.java:4467)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.view.View$PerformClick.run(View.java:18776)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.os.Handler.handleCallback(Handler.java:808)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.os.Handler.dispatchMessage(Handler.java:103)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.os.Looper.loop(Looper.java:193)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at android.app.ActivityThread.main(ActivityThread.java:5292)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
10-29 19:30:59.402: E/SQLiteDatabase(11765): at dalvik.system.NativeStart.main(Native Method)
10-29 19:30:59.473: E/RemoteViews(720): ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
10-29 19:30:59.475: E/RemoteViews(720): ANR Warning,RemoteViews can only be used once ,if not ,it may cause ANR in hosts such as Laucher,SystemUI. keys for search <ANR Exception MSG History>
Hey friend -: see this that I have declared -:
private static final int DATABASE_VFERSION =1;// change it every single time u are changing the filefds of hte databadse
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";// name of the database
public static final String COLUMN_ID = "_id";// for each col u have to giuve the name
public static final String COLUMN_PRODUCTNMAE = "productname";
Aucun commentaire:
Enregistrer un commentaire