Reputation: 139
I've been trying to create a DB with sqlite in my android project and don't understand why I get this msg error "no such table" :S i thought i was creating the DB good. Any help?
here's my code handler_sqlite class :
public class Handler_sqlite extends SQLiteOpenHelper {
public Handler_sqlite(Context ctx)
{
super ( ctx, "MiBase", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db )
{
String query = " CREATE TABLE IF NOT EXISTS usuarios ( user VARCHAR PRIMARY KEY, password VARCHAR );" ;
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int version_ant, int version_post)
{
db.execSQL("DROP TABLE IF EXISTS usuarios");
onCreate(db);
}
public void insertarReg(String user, String password)
{
ContentValues valores = new ContentValues();
valores.put("user", user);
valores.put("password", password);
this.getWritableDatabase().insert("usuarios",null,valores);
}
public void abrir()
{
this.getWritableDatabase();
}
public void cerrar()
{
this.close();
}
public String leer_user ()
{
String columnas[]={"user","password"};
Cursor c = this.getReadableDatabase().query("usuarios",columnas,null,null,null,null,null);
int iu;
iu = c.getColumnIndex("user");
c.moveToLast();
return c.getString(iu);
}
public String leer_password ()
{
String columnas[]={"user","password"};
Cursor c = this.getReadableDatabase().query("usuarios",columnas,null,null,null,null,null);
int ip;
ip = c.getColumnIndex("password");
c.moveToLast();
return c.getString(ip);
}
}
and my main code that references to the bd :
Handler_sqlite helper = new Handler_sqlite(this);
helper.abrir();
helper.insertarReg(username,passw);
String username2 = helper.leer_user();
String password2 = helper.leer_password();
current error :
error inserting password = x user = y android.database.sqlite.SQLiteException: no such table: usuarios, while compiling: INSERT INTO usuarios (password,user) Values (?,?);
thanks in advance.
Upvotes: 0
Views: 2014
Reputation: 13223
The name of your database should contain the .db extension (i.e. MiBase.db) when you pass it to the constructor.
Upvotes: 1
Reputation: 636
Are you sure the create table statement is correct ? (ie the exec doesn't throw any exceptions/errors/etc).
Does the DB actually exist ? If you adb shell and run-as your app, look in the db directory (name may change from version to version) and see if there is a usarios.sqlite file.
Are you sure no typos for the table name in all your code ? (ie use a constant instead of re-typing it all over)
Upvotes: 0