嗨,我在android中創建一個簡單的SQLite數據庫。SQLite數據庫(爲什麼要這樣做?)
我使用SQLite輔助類
package com.and.example.helper;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DatabaseHelper {
private static final String DATABASE_NAME = "mydata10.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "mytable10";
private SQLiteDatabase db;
private Context context;
private SQLiteStatement insertStm;
private static final String INSERT = "insert into " + TABLE_NAME + "(name,latitude,longitude,address,contectno) values(?,?,?,?,?)";
public DatabaseHelper (Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
//db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
this.insertStm = this.db.compileStatement(INSERT);
}
private static class OpenHelper extends SQLiteOpenHelper {
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT, letitude REAL, longitude REAL, address TEXT, contectno INTEGER)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop table and recreate");
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
public OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
public long insert(String name, double latitude, double longitude, String address, int contactno){
this.insertStm.bindString(1, name);
this.insertStm.bindDouble(2, latitude);
this.insertStm.bindDouble(3, longitude);
this.insertStm.bindString(4, address);
this.insertStm.bindLong(5, contactno);
return this.insertStm.executeInsert();
}
public void deleteAll(){
this.db.delete(TABLE_NAME, null, null);
}
public List<String>selectAll(){
List<String>list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[]{"name","latitude","longitude","address","contectno"}, null, null, null, null, null);
if(cursor.moveToFirst())
{
do {
list.add(cursor.getString(0));
list.add(cursor.getString(1));
list.add(cursor.getString(2));
list.add(cursor.getString(3));
list.add(cursor.getString(4));
}while(cursor.moveToNext());
}
if(cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
}
我的主要Activity類
package com.and.example.database;
import java.util.List;
import com.and.example.helper.DatabaseHelper;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class DatabaseDemoActivity extends Activity{
private TextView textout;
private DatabaseHelper sqlHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textout = (TextView)findViewById(R.id.textview1);
sqlHelper = new DatabaseHelper(this);
sqlHelper.deleteAll();
sqlHelper.insert("Durgesh",1.352566007,103.78921587,"ahmedabad",942974);
// sqlHelper.insert("Mukesh");
List<String>names = this.sqlHelper.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Name in Database: \n");
for(String name : names)
{
sb.append(name + "\n");
}
Log.d("Example", "name size - " +names.size());
this.textout.setText(sb.toString());
}
}
,並運行此項目,然後生成一個錯誤的logcat
九月3日至8日:16:57.963 :錯誤/ AndroidRuntime(1003):致命例外:主 03-08 09:16:57.963:錯誤/ AndroidRuntime(1003):java.lang.RuntimeException:無法啓動活動ComponentInfo {co m.and.example.database/com.and.example.database.DatabaseDemoActivity}:android.database.sqlite.SQLiteException:表mytable10沒有名爲latitude:的列,而編譯時:insert into mytable10(name,latitude,longitude,address (1003):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-Jun-03:09:16:57.963:ERROR/AndroidRuntime(1003) 08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.app .ActivityThread.access $ 2300(ActivityThread.java:125) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.os.Handler.dispatchMessage(Han dler.java:99) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.os.Looper.loop(Looper.java:123) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.app.ActivityThread.main(ActivityThread.java:4627) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at java.lang.reflect.Method.invokeNative(Native Method ) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at java.lang.reflect.Method.invoke(Method.java:521) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003) ):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:626) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at dalvik.system.NativeStart.main(Native Method) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):引起:android.database.sqlite.SQLiteException:表mytable10沒有名爲latitude:的列,編譯時:insert into mytable10(name,latitude,longitude,地址,contectno)值(?,?,?,?,?) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 03- 08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android SQLiteCompiledSql.java:64 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:80) 03- 08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.database.sqlite.SQLite Statement(SQLiteStatement.java:36) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145) 03-08 09:16 :57.963:ERROR/AndroidRuntime(1003):at com.and.example.helper.DatabaseHelper。(DatabaseHelper.java:31) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at com.and。 example.database.DatabaseDemoActivity.onCreate(DatabaseDemoActivity.java:23) 03-08 09:16:57.963:ERROR/AndroidRuntime(1003):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-08 09:16:57.963:錯誤/ AndroidRuntime(1003):在android.app.ActivityThread.performLaunchActivity(ActivityThread。的java:2627) 9月3日至8日:16:57.963:ERROR/AndroidRuntime(1003):... 11更
然後我解決錯誤
db.execSQL( 「CREATE TABLE」 + + TABLE_NAME 「(id INTEGER PRIMARY KEY,name TEXT,latitude REAL,longitude REAL,address TEXT,contectno INTEGER)」);
並運行該項目,但在LogCat中生成相同的錯誤。
然後我改變了唯一的數據庫名稱和表名其他代碼是相同的,併成功運行項目。
我的問題是 爲什麼這樣?