android-“表X没有名为Y的列”将数据插入SQLite数据库时出错

android-“表X没有名为Y的列”将数据插入SQLite数据库时出错,第1张

概述我在数据库插入数据时出错.01-2820:59:06.277:I/Database(553):sqlitereturned:errorcode=1,msg=tabletableKohasnocolumnnamedphone01-2820:59:06.309:E/Database(553):Errorinsertingphone=email=address=name=01-2820:59:06.309:E/Database(55

我在数据库上插入数据时出错.

01-28 20:59:06.277: I/Database(553): sqlite returned: error code = 1, msg = table tableKo has no column named phone01-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.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();    }}

我的代码有什么问题?在此先感谢您的帮助

解决方法:

您错过了一个空格:

代替

Col_PHONE + "text not null,"

采用

Col_PHONE + " text not null,"
总结

以上是内存溢出为你收集整理的android-“表X没有名为Y的列”将数据插入SQLite数据库时出错全部内容,希望文章能够帮你解决android-“表X没有名为Y的列”将数据插入SQLite数据库时出错所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1211118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存