我在数据库中插入数据时遇到错误。
01-28 20:59:06.277: I/Database(553): sqlite returned: error code = 1, msg = table tableKo has no column named phone
01-28 20:59:06.309: E/Database(553): Error inserting phone= email= address= name=
01-28 20:59:06.309: E/Database(553): android.database.sqlite.SQLiteException: table tableKo has no column named phone: , while compiling: INSERT INTO tableKo(phone, email, address, name) VALUES(?, ?, ?, ?);
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
01-28 20:59:06.309: E/Database(553): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
01-28 20:59:06.309: E/Database(553): at com.example.databasetest.DBHandler.insertData(DBHandler.java:76)
01-28 20:59:06.309: E/Database(553): at com.example.databasetest.MainActivity.saveButtonHandler(MainActivity.java:39)
01-28 20:59:06.309: E/Database(553): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 20:59:06.309: E/Database(553): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 20:59:06.309: E/Database(553): at android.view.View$1.onClick(View.java:2067)
01-28 20:59:06.309: E/Database(553): at android.view.View.performClick(View.java:2408)
01-28 20:59:06.309: E/Database(553): at android.view.View$PerformClick.run(View.java:8816)
01-28 20:59:06.309: E/Database(553): at android.os.Handler.handleCallback(Handler.java:587)
01-28 20:59:06.309: E/Database(553): at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 20:59:06.309: E/Database(553): at android.os.Looper.loop(Looper.java:123)
01-28 20:59:06.309: E/Database(553): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-28 20:59:06.309: E/Database(553): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 20:59:06.309: E/Database(553): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 20:59:06.309: E/Database(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-28 20:59:06.309: E/Database(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-28 20:59:06.309: E/Database(553): at dalvik.system.NativeStart.main(Native Method)
我使用一个单独的类来扩展SQLiteOpenHelper来处理我的数据库。我已经在互联网上搜索了解我的表格如何没有列名称是我的代码: 我的主要课程
package com.example.databasetest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText nameET;
EditText addressET;
EditText phoneET;
EditText emailET;
String name;
String address;
String phone;
String email;
DBHandler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameET = (EditText)findViewById(R.id.nameET);
addressET = (EditText)findViewById(R.id.addressET);
phoneET = (EditText)findViewById(R.id.phoneET);
emailET = (EditText)findViewById(R.id.emailET);
name = nameET.getText().toString();
address = addressET.getText().toString();
phone = phoneET.getText().toString();
email = emailET.getText().toString();
handler = new DBHandler(this);
}
public void saveButtonHandler(View v) {
handler.insertData(name, address, phone, email);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是我的数据库处理程序类
package com.example.databasetest;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHandler {
public static final String TABLE_NAME = "tableKo";
public static final String DATABASE_NAME = "databaseKo";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBHandler";
public static final String COL_ID = "_id";
public static final String COL_NAME = "name";
public static final String COL_ADDRESS = "address";
public static final String COL_PHONE = "phone";
public static final String COL_EMAIL = "email";
private final Context context;
private SQLiteDatabase db;
private MySQLiteOpenHelper DBHelper;
private static final String CREATE_DATABASE ="create table "
+ TABLE_NAME + "(" + COL_ID
+ " integer primary key, " + COL_NAME
+ " text not null, " + COL_ADDRESS + "text not null,"
+ COL_PHONE + "text not null," + COL_EMAIL + "text not null);";
public DBHandler(Context ctx) {
this.context = ctx;
DBHelper = new MySQLiteOpenHelper(context);
}
private static class MySQLiteOpenHelper extends SQLiteOpenHelper{
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(CREATE_DATABASE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public DBHandler open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public void insertData (String name, String address, String phone, String email) {
open();
ContentValues values = new ContentValues();
values.put(COL_NAME, name);
values.put(COL_ADDRESS, address);
values.put(COL_PHONE, phone);
values.put(COL_EMAIL, email);
db.insert(TABLE_NAME, null, values);
//db.execSQL("Insert into " +TABLE_NAME+ " VALUES('"+COL_ID+"','"+name+"','"+address+"','"+phone+"','"+email+"');");
db.close();
}
}
我的代码出了什么问题?在此先感谢您的帮助
答案 0 :(得分:4)
而不是
COL_PHONE + "text not null,"
使用
COL_PHONE + " text not null,"
答案 1 :(得分:0)
通常可以通过删除设备上的应用并重新安装来解决此问题。首先根据数据库中的列名检查代码中的列名称,如果它们不匹配则意味着您仍旧您的设备上的数据库副本,您在进行更改后尚未更改。