Force close when get values from sqlite
It's my first time to dev android, also SQLite. I follow some tutorials to get values from SQLite. This is my code:
Edit:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myManager = (LocationManager) getSystemService(LOCATION_SERVICE);
tv = (TextView) findViewById(R.id.test);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = ((Double) location.getLatitude()).toString();
longitude = ((Double) location.getLongitude()).toString();
date = getDate();
time = getCurrentTime();
if (location != null) {
if (checkInternet()) {
String s = "";
s += "\tLatitude: " + latitude + "\n";
s += "\tLongitude: " + longitude + "\n";
s += "\tDate: " + date + "\n";
s += "\tTime: " + time + "\n";
tv.setText(s);
//lookupData();
}
} else {
Toast.makeText(this, "network connection is not available",
Toast.LENGTH_SHORT).show();
keepSQLite();
}
}
}
public void keepSQLite() {
try {
locationDB = openOrCreateDatabase("locationDB", MODE_PRIVATE, null);
createTable();
insertTable();
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(),
"Could not create or Open the database");
} finally {
locationDB.close();
}
}
private void createTable() {
locationDB.execSQL("CREATE TABLE IF NOT EXISTS " + "ben_logs_boss"
+ " (username VARCHAR, " + " date VARCHAR, " + "time VARCHAR, "
+ " lat VARCHAR, " + " lng VARCHAR);");
}
private void insertTable() {
locationDB.execSQL("INSERT INTO " + "ben_logs_boss" + " Values ("
+ "ben" + "," + getDate() + "," + getDate() + ","
+ getCurrentTime() + "," + latitude + "," + longitude + ");");
Toast.makeText(this, "insert", Toast.LENGTH_SHORT).show();
}
private void lookupData() {
**String DB_PATH = "/data/data/test.test.test/databases/";
String DB_NAME = "ben_logs_boss";
String myPath = DB_PATH + DB_NAME;
locationDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);**
cursor = locationDB.rawQuery("SELECT lat,lng FROM " + "ben_logs_boss",
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
cursor.getString(cursor.getColumnIndex("lat"));
cursor.getString(cursor.getColumnIndex("lng"));
} while (cursor.moveToNext());
}
cursor.close();
}
locationDB.close();
}
When application run until the step that call this method, force close occurred. I don't know why. Anyone can tell me the mistake(s) ? Thank you.
Update Logcat:
When it call lookupData():
08-10 14:48:12.289 I/ActivityManager( 129): Displayed test.test.test/.TestActivity: +454ms
08-10 14:48:42.843 E/Database(22387): sqlite3_open_v2("/data/data/test.test.test/database/ben_logs_boss.sqlite", &handle, 1, NULL) failed
08-10 14:48:51.425 E/InputDispatcher( 129): channel '40855ac0 test.test.test/test.test.test.TestActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
08-10 14:48:51.425 E/InputDispatcher( 129): channel '40855ac0 test.test.test/test.test.test.TestActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
开发者_C百科
when it call keepSQLite():
08-10 14:50:29.605 E/TestActivity(22497): Could not create or Open the database
08-10 14:50:31.753 V/WindowManager( 129): isSystemKeyEventRequested() is called keyCode = 3 componentName = ComponentInfo{test.test.test/test.test.test.TestActivity}
08-10 14:50:32.253 E/Database(22610): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
08-10 14:50:32.253 E/Database(22610): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
i think you are missing your database extension ie(.sqlite)
String DB_NAME = "ben_logs_boss.sqlite";
精彩评论