mercredi 11 février 2015

Can't insert data into sqlite in Android

This is my database class i create separate class for db and assign their column name through variables. but when im try to add data its says "their is no such table as entry1." but the same senario work for amount table. cant seem to identify the problem



public class DBhelper extends SQLiteOpenHelper{

static final String DATABASE = "money.db";
static final int VERSION = 1;
static final String TABLE = "amount";
static final String TABLE1 = "entry1";
static final String C_ID = "_id";
static final String C_AMOUNT = "amout";
static final String C_DATE = "_date";
static final String C_TIME = "_time";

static final String C_DISCRIPTION = "discription1";

public DBhelper(Context context) {
super(context, DATABASE, null, VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE+ "(" +C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +C_AMOUNT+ " text,"
+C_DATE+ " text," +C_TIME+ " text )");

db.execSQL("CREATE TABLE" + TABLE1+ "(" +C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +C_AMOUNT+ " text,"
+C_DISCRIPTION+ " text," +C_DATE+ " text," +C_TIME+ " text )");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

db.execSQL("drop table" + TABLE);
db.execSQL("drop table" + TABLE1);
onCreate(db);

}

}


And this is where I add details:



public class addentry extends Activity implements OnClickListener{



int HOUR, MINUTE;
String formattedDate, time;
DBhelper helper;
SQLiteDatabase db;
Cursor c1;
TextView lbl;
String amount;
Button view,entry;
EditText txtentry,dis;

@Override
protected void onCreate(Bundle savedInstanceState) {


// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.entry);

helper = new DBhelper(this);
Calendar c = Calendar.getInstance();


SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = df.format(c.getTime());

lbl = (TextView) findViewById(R.id.lblamount1);

view = (Button) findViewById(R.id.viewentry);
entry = (Button) findViewById(R.id.btnaddentry);
txtentry = (EditText) findViewById(R.id.txtentry);
dis = (EditText) findViewById(R.id.txtdis);

MINUTE = c.get(Calendar.MINUTE);
HOUR = c.get(Calendar.HOUR);
time = (Integer.toString(HOUR) + " : " + Integer.toString(MINUTE));


view.setOnClickListener(this);
entry.setOnClickListener(this);

}


private void clearfield() {

dis.setText("");
txtentry.setText("");
}





@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
{
return (keyCode == KeyEvent.KEYCODE_BACK ? true : super.onKeyDown(keyCode, event));
}
}




@Override
public void onClick(View v) {
if(v == view){
startActivity(new Intent(addentry.this, viewentry.class));
}

if(v == entry){
ContentValues value = new ContentValues();
value.put(DBhelper.C_AMOUNT, txtentry.getText().toString());
value.put(DBhelper.C_DISCRIPTION, dis.getText().toString());
value.put(DBhelper.C_DATE, formattedDate);
value.put(DBhelper.C_TIME, time);

db = helper.getWritableDatabase();
db.insert(DBhelper.TABLE1, null, value);
db.close();
clearfield();
Toast.makeText(this, "entry add succecsfully", Toast.LENGTH_LONG)
.show();

}

}

}


but i got this in my logcat



02-12 02:06:38.490: E/SQLiteLog(9267): (1) no such table: entry1

02-12 02:06:38.540: E/SQLiteDatabase(9267): Error inserting _date=12-Feb-2015 discription1=ff amout=d _time=2 : 6
02-12 02:06:38.540: E/SQLiteDatabase(9267): android.database.sqlite.SQLiteException: no such table: entry1 (code 1): , while compiling: INSERT INTO entry1(_date,discription1,amout,_time) VALUES (?,?,?,?)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:949)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:560)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1495)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1367)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at com.app.addentry.onClick(addentry.java:112)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.view.View.performClick(View.java:4101)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.view.View$PerformClick.run(View.java:17082)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.os.Handler.handleCallback(Handler.java:615)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.os.Handler.dispatchMessage(Handler.java:92)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.os.Looper.loop(Looper.java:137)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at android.app.ActivityThread.main(ActivityThread.java:4950)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
02-12 02:06:38.540: E/SQLiteDatabase(9267): at dalvik.system.NativeStart.main(Native Method)
02-12 02:12:42.370: D/dalvikvm(9267): WAIT_FOR_CONCURRENT_GC blocked 0ms
02-12 02:12:42.440: D/dalvikvm(9267): GC_EXPLICIT freed 116K, 10% free 5784K/6407K, paused 13ms+4ms, total 71ms

Aucun commentaire:

Enregistrer un commentaire