I'm trying create a connection with sqlite in my app, but if i execute the app, in line getWritableDatabase, the apps force close, i don't understand what's happening.
DBCore class:
public class DBCore extends SQLiteOpenHelper{
private static final String DB_NAME = "";
private static final int DB_VERSION = 1;
public DBCore(Context ctx){
super(ctx, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table user(code integer primary key, pass text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table user;");
onCreate(db);
}
}
DBConnection class:
public class DBLiteConnection {
private SQLiteDatabase db;
public DBLiteConnection(Context ctx){
DBCore dbcore = new DBCore(ctx);
db = dbcore.getWritableDatabase();
}
public void insert(int code, int pass){
ContentValues values = new ContentValues();
values.put("code", code);
values.put("pass", pass);
db.insert("user", null, values);
}
public void update(int code, int pass){
ContentValues values = new ContentValues();
values.put("code", code);
values.put("pass", pass);
db.update("user", values, "_code = "+code, null);
}
public void delete(int code, int pass){
ContentValues values = new ContentValues();
values.put("code", code);
values.put("pass", pass);
db.delete("user", "code = "+code, null);
}
public List<DBLiteUser> search(){
List<DBLiteUser> list = new ArrayList<DBLiteUser>();
String[] columns = new String[]{"code","pass"};
Cursor cursor = db.query("user", columns, null, null, null, null, null);
if(cursor.getCount() > 0){
cursor.moveToFirst();
DBLiteUser user = new DBLiteUser();
user.setCode(cursor.getInt(0));
user.setPass(cursor.getString(1));
list.add(user);
}
return list;
}
}
Force close in this line "db = dbcore.getWritableDatabase();"
log:
02-02 11:35:21.778: E/AndroidRuntime(2593): FATAL EXCEPTION: main
02-02 11:35:21.778: E/AndroidRuntime(2593): Process: com.example.newpointerbi, PID: 2593
02-02 11:35:21.778: E/AndroidRuntime(2593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpointerbi/com.example.newpointerbi.LoadingApp_Activity}: java.lang.StringIndexOutOfBoundsException: length=0; index=0
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread.access$800(ActivityThread.java:142)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.os.Handler.dispatchMessage(Handler.java:102)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.os.Looper.loop(Looper.java:136)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread.main(ActivityThread.java:5118)
02-02 11:35:21.778: E/AndroidRuntime(2593): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 11:35:21.778: E/AndroidRuntime(2593): at java.lang.reflect.Method.invoke(Method.java:515)
02-02 11:35:21.778: E/AndroidRuntime(2593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
02-02 11:35:21.778: E/AndroidRuntime(2593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
02-02 11:35:21.778: E/AndroidRuntime(2593): at dalvik.system.NativeStart.main(Native Method)
02-02 11:35:21.778: E/AndroidRuntime(2593): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=0
02-02 11:35:21.778: E/AndroidRuntime(2593): at java.lang.String.indexAndLength(String.java:584)
02-02 11:35:21.778: E/AndroidRuntime(2593): at java.lang.String.charAt(String.java:578)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ContextImpl.validateFilePath(ContextImpl.java:2260)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1053)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-02 11:35:21.778: E/AndroidRuntime(2593): at com.example.newpointerbi.DBLiteConnection.<init>(DBLiteConnection.java:16)
02-02 11:35:21.778: E/AndroidRuntime(2593): at com.example.newpointerbi.LoadingApp_Activity.onCreate(LoadingApp_Activity.java:19)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.Activity.performCreate(Activity.java:5275)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 11:35:21.778: E/AndroidRuntime(2593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
02-02 11:35:21.778: E/AndroidRuntime(2593): ... 11 more
where's the error?
Aucun commentaire:
Enregistrer un commentaire