这是我第一次开发android,也是SQLite。我跟随 一些从SQLite获取值的教程。这是我的代码:
修改
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();
}
当应用程序运行到调用此方法的步骤时,强制关闭发生。 我不知道为什么。谁能告诉我错误?谢谢。
更新Logcat:
调用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!
调用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)
答案 0 :(得分:1)
我认为您缺少数据库扩展名,即(.sqlite)
String DB_NAME = "ben_logs_boss.sqlite";