【发布时间】:2011-03-08 04:28:39
【问题描述】:
您好,我正在 android 中创建一个简单的 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;
}
}
我的主要活动类
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
03-08 09:16:57.963: 错误/AndroidRuntime(1003): 致命异常: main 03-08 09:16:57.963: 错误/AndroidRuntime(1003): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.and.example.database/com.and.example.database.DatabaseDemoActivity}: android.database .sqlite.SQLiteException: table mytable10 has no column named latitude: ,同时编译:插入 mytable10(name,latitude,longitude,address,contectno) values(?,?,?,?,?) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.os.Handler.dispatchMessage(Handler.java:99) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.os.Looper.loop(Looper.java:123) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 java.lang.reflect.Method.invoke(Method.java:521) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-08 09:16:57.963:错误/AndroidRuntime(1003):在 dalvik.system.NativeStart.main(本机方法) 03-08 09:16:57.963: ERROR/AndroidRuntime(1003): Caused by: android.database.sqlite.SQLiteException: table mytable10 has no column named latitude: , while compile: insert into mytable10(name,latitude,longitude,address ,contectno) 值(?,?,?,?,?) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:36) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 com.and.example.helper.DatabaseHelper.(DatabaseHelper.java:31) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 com.and.example.database.DatabaseDemoActivity.onCreate(DatabaseDemoActivity.java:23) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 03-08 09:16:57.963: 错误/AndroidRuntime(1003): ... 11 更多
然后我解决错误
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT, latitude REAL, longitude REAL, address TEXT, contectno INTEGER)" );
并运行项目,但在 LogCat 中生成相同的错误。
然后我更改唯一的数据库名和表名,其他代码相同并成功运行项目。
我的问题是 为什么会这样?
【问题讨论】:
标签: android