Reputation: 1115
I am getting a table not found error. I googled a lot but not able to found the source of the error
public class update extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
Button upbtn=(Button)findViewById(R.id.upbutton);
final EditText detedit=(EditText)findViewById(R.id.upedit);
final EditText amtedit=(EditText)findViewById(R.id.editText1);
stdb obj=new stdb(this);
final SQLiteDatabase dobj=obj.getWritableDatabase();
upbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String setdet,setamt;
setdet=detedit.getText().toString();
setamt=amtedit.getText().toString();
//try{
String insertQuery = "INSERT INTO " +stdb.tname+" (" + stdb.details+" , "+stdb.amt+") VALUES("+setdet+" , "+setamt+");";
//Log.e("sql",insertQuery);
dobj.execSQL(insertQuery);
//}
//catch(SQLException e ){
// Log.e("sqlite",e.toString());
//}
}
});
}
}
SQLiteOpenHelper:
public class stdb extends SQLiteOpenHelper {
private static final int version = 1;
public static final String dname = "statments.db";
public static final String tname="transactions";
public static final String sid = "sno";
public static final String details = "details";
public static final String amt = "amount";
public stdb(Context context) {
super(context, dname, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String ex="CREATE TABLE " + tname + " (" + sid + " INTEGER PRIMARY KEY AUTOINCREMENT," + details+ " VARCHAR(255),"+amt+" INTEGER);";
Log.e("ex",ex );
db.execSQL("CREATE TABLE " + tname + " (" + sid + " INTEGER PRIMARY KEY AUTOINCREMENT," + details+ " VARCHAR(255),"+amt+" INTEGER);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ tname);
onCreate(db);
}
}
Upvotes: 0
Views: 1436
Reputation: 86948
Try incrementing the version number:
private static final int version = 2;
I'm guessing that you changed your CREATE TABLE
statement at some point, but the system won't know about these changes automatically. The easiest way to update your schema is to add 1 to version
.
Also
You shouldn't use execSQL() for INSERT
commands, use insert():
ContentValues initialValues = new ContentValues();
initialValues.put(stbd.details, setdet);
initialValues.put(stbd.amt, setamt);
dobj.insert(stbd.tname, null, initialValues);
Or rawQuery():
String insertQuery = "INSERT INTO " +stdb.tname+" (" + stdb.details+" , "+stdb.amt+") VALUES(?, ?);";
dobj.rawQuery(insertQuery, new String[] {setdet, setamt});
Upvotes: 1