安卓:表中没有指定列"变量名" MYSQL数据库错误 [英] Android : Table has no column named "variable name" MySql Database error

查看:127
本文介绍了安卓:表中没有指定列"变量名" MYSQL数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到并且像一个错误显示belowe当我试图把我的数据库中的一个新条目。我搜索槽了好几个小时了,但我不能够探测到什么是错。任何投入将是极好的!

下面是LogCat中在错误。

  02-27 23:02:51.451:E / SQLiteLog(6777):(1)表dager没有指定的列brutto
02-27 23:02:51.451:E / SQLiteDatabase(6777):错误插入brutto = 0日期= 2013年3月21日
小时= 4
02-27 23:02:51.451:E / SQLiteDatabase(6777):android.database.sqlite.SQLiteException:
表dager没有指定的列brutto(code 1):在编制:INSERT INTO
dager(brutto,日期,时间)VALUES(?,?,?)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在
android.database.sqlite.SQLiteConnection.native prepareStatement(本机方法)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteConnection.acquire preparedStatement(SQLiteConnection.java:882)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteConnection prepare(SQLiteConnection.java:493)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteSession prepare(SQLiteSession.java:588)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteProgram< INIT>(SQLiteProgram.java:58)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteStatement< INIT>(SQLiteStatement.java:31)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在com.adev.timelonn.DatabaseHandler.addDay(DatabaseHandler.java:79)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在com.adev.timelonn.AddHours.onClick(AddHours.java:99)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.view.View.performClick(View.java:4084)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.view.View $ PerformClick.run(View.java:16966)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.os.Handler.handleCallback(Handler.java:615)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.os.Handler.dispatchMessage(Handler.java:92)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.os.Looper.loop(Looper.java:137)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在android.app.ActivityThread.main(ActivityThread.java:4931)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在java.lang.reflect.Method.invokeNative(本机方法)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在java.lang.reflect.Method.invoke(Method.java:511)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-27 23:02:51.451:E / SQLiteDatabase(6777):在dalvik.system.NativeStart.main(母语
方法)
 

我的数据库文件

 公共类数据库处理器扩展SQLiteOpenHelper {
 私有静态最终诠释DATABASE_VERSION = 1;

 //数据库名称
 私有静态最后弦乐DATABASE_NAME =timeliste;

 //联系方式表名
 私有静态最后弦乐TABLE_DAYS =dager;

 //联系方式表列名
 私有静态最后弦乐KEY_ID =ID;
 私有静态最后弦乐KEY_DATE =日期;
 私有静态最后弦乐KEY_HOURS =小时;
 私有静态最后弦乐KEY_UB =UB;
 私有静态最后弦乐KEY_BRUTTO =brutto;

 公共数据库处理器(上下文的背景下){
  超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
 }

 //创建表
 @覆盖
  公共无效的onCreate(SQLiteDatabase DB)
 字符串CREATE_DAY_TABLE =CREATE TABLE+ TABLE_DAYS +(
 + KEY_ID +INTEGER PRIMARY KEY,+ KEY_DATE +TEXT+ KEY_HOURS +INTEGER,
     
 + KEY_UB +INTEGER,+ KEY_BRUTTO +INTEGER,+);
 db.execSQL(CREATE_DAY_TABLE);
 }

 //升级数据库
 @覆盖
 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
  如果存在//删除旧的表
  db.execSQL(DROP TABLE IF EXISTS+ TABLE_DAYS);

  //再次创建表
  的onCreate(DB);
 }

 / **
  *所有CRUD(创建,读取,更新,删除)操作
  * /

 //添加新联系人
 无效addDay(AddNewDay newday){
  SQLiteDatabase DB = this.getWritableDatabase();


  ContentValues​​值=新ContentValues​​();
  values​​.put(KEY_DATE,newday.getDate()); // GETDATE
  values​​.put(KEY_BRUTTO,newday.getBrutto()); //GetBruttoLønn
  values​​.put(KEY_HOURS,newday.getHours()); // GetHours
  values​​.put(KEY_UB,newday.getUb()); // GetUBTillegg

  //插入行
  db.insert(TABLE_DAYS,空,价值观);
  db.close(); //闭幕数据库连接
 }

 //获取单日
 AddNewDay getContact(INT ID){
  SQLiteDatabase DB = this.getReadableDatabase();

  光标光标= db.query(TABLE_DAYS,新的String [] {KEY_ID,
    KEY_DATE,KEY_HOURS,KEY_BRUTTO,KEY_UB},KEY_ID +=?,
    新的String [] {将String.valueOf(ID)},NULL,NULL,NULL,NULL);
  如果(光标!= NULL)
   cursor.moveToFirst();

  AddNewDay newday =新AddNewDay(的Integer.parseInt(cursor.getString(0)),
    cursor.getString(1),的Integer.parseInt(cursor.getString(2)),
    的Integer.parseInt(cursor.getString(3)),的Integer.parseInt(cursor.getString(4)));
  //返回接触
  返回newday;
 }

 //获取所有联系人
 公开名单< AddNewDay> getAllContacts(){
  名单< AddNewDay> contactList =新的ArrayList< AddNewDay>();
  //选择所有查询
  字符串selectQuery =SELECT * FROM+ TABLE_DAYS;

  SQLiteDatabase DB = this.getWritableDatabase();
  光标光标= db.rawQuery(selectQuery,NULL);

  //遍历所有行和添加到列表
  如果(cursor.moveToFirst()){
   做 {
    AddNewDay天=新AddNewDay();
    days.setID(的Integer.parseInt(cursor.getString(0)));
    days.setDate(cursor.getString(1));
    days.setHours(的Integer.parseInt(cursor.getString(2)));
    days.setUb(的Integer.parseInt(cursor.getString(3)));
    days.setBrutto(的Integer.parseInt(cursor.getString(4)));
    //添加联系人列出
    contactList.add(天);
   }而(cursor.moveToNext());
  }

  //返回联系人列表
  返回contactList;
 }

 //更新单触点
 公众诠释updateDay(AddNewDay newday){
  SQLiteDatabase DB = this.getWritableDatabase();

  ContentValues​​值=新ContentValues​​();
  values​​.put(KEY_ID,newday.getID());
  values​​.put(KEY_DATE,newday.getDate());
  values​​.put(KEY_HOURS,newday.getHours());
  values​​.put(KEY_UB,newday.getUb());
  values​​.put(KEY_BRUTTO,newday.getUb());

  //更新行
  返回db.update(TABLE_DAYS,价值观,KEY_ID +=?,
    新的String [] {将String.valueOf(newday.getID())});
 }

 //删除单个联系人
 公共无效deleteDay(AddNewDay newday){
  SQLiteDatabase DB = this.getWritableDatabase();
  db.delete(TABLE_DAYS,KEY_ID +=?,
    新的String [] {将String.valueOf(newday.getID())});
  db.close();
 }

 //获取联系人计数
 公众诠释getContactsCount(){
  字符串countQuery =SELECT * FROM+ TABLE_DAYS;
  SQLiteDatabase DB = this.getReadableDatabase();
  光标光标= db.rawQuery(countQuery,NULL);
  cursor.close();

  //返回计数
  返回cursor.getCount();
 }


}
 

然后我的 AddNewDay文件

 公共类AddNewDay {

 //私有变量
     INT _id;
     字符串_date;
     INT _hours;
     INT _ubtillegg;
     INT _brutto;

     //空的构造
     公共AddNewDay(){
     }
     //构造
     公共AddNewDay(INT ID,字符串日期,INT小时,INT ubtillegg,诠释brutto){
      this._id = ID;
      this._date =日期;
      this._hours =小时;
      this._ubtillegg = ubtillegg;
      this._brutto = brutto;
     }
     //构造
     公共AddNewDay(字符串日期,INT小时,INT brutto){
      this._date =日期;
      this._hours =小时;
      this._brutto = brutto;
     }
     //获取ID
     公众诠释的getID(){
      返回this._id;
     }

     //设置ID
     公共无效SETID(INT ID){
      this._id = ID;
     }

     //获取日期
     公共字符串GETDATE(){
      返回this._date;
     }

     //设置日期
     公共无效的setDate(字符串日期){
      this._date =日期;
     }

     //获取时间
     公众诠释getHours(){
      返回this._hours;
     }

     //设置小时
     公共无效,调用setHours(INT小时){
      this._hours =小时;
     }

     //获取ubtillegg
     公众诠释getUb(){
      返回this._ubtillegg;
     }

     //设置ubtillegg
     公共无效setUb(INT UB){
      this._ubtillegg = UB;
     }

     //获取brutto
     公众诠释getBrutto(){
      返回this._brutto;
     }

     //设置brutto
     公共无效setBrutto(INT brutto){
      this._brutto = brutto;
     }

             公共字符串的toString(){
         返回_date +jobbet杜+ _hours +计时器。
             }
            }
 

这就是我加入数据库中的新的条目。

  //#使newday的新对象。
        AddNewDay newday =新AddNewDay();
        newday._date =2013年3月21日;
        newday._id = 1;
        newday._hours = 4;
        newday._ubtillegg = 1500;

        //#打开DATABSE连接,并写入新的一天。
        数据库处理器连接=新的数据库处理器(本);
        connect.addDay(newday);

        //#更新与项DBLIST。
        GlobalVariables.dblist = connect.getAllContacts();
 

解决方案

所以basicly我找到了解决办法。我仍然困惑的是多么愚蠢了。并明确数据库的工作,我的超敏感材料!我的问题是我忘了最后的整数值的空白。因此,而不是整数我写了整数,这给了我一个行名为bruttoINTEGER,而不是brutto。因此,一个白色的空间是错误。我改写了CREATETABLE功能如下:

 公共无效的onCreate(SQLiteDatabase DB){
   字符串CREATE_DAY_TABLE =CREATE TABLE+ TABLE_DAYS +(
 + KEY_ID +INTEGER PRIMARY KEY,+ KEY_DATE +TEXT+ KEY_HOURS +INTEGER
 + KEY_UB +INTEGER,+ KEY_BRUTTO +INTEGER+);;
 db.execSQL(CREATE_DAY_TABLE);
 }
 

I'm getting and error like the one showed belowe when I try to put a new entry in my database. I search trough it for hours now, but I'm not able to detect whats wrong. Any input would be superb!

Here is the error from LogCat.

02-27 23:02:51.451: E/SQLiteLog(6777): (1) table dager has no column named brutto
02-27 23:02:51.451: E/SQLiteDatabase(6777): Error inserting brutto=0 date=21.03.2013      
hours=4
02-27 23:02:51.451: E/SQLiteDatabase(6777): android.database.sqlite.SQLiteException:    
table dager has no column named brutto (code 1): , while compiling: INSERT INTO     
dager(brutto,date,hours) VALUES (?,?,?)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at 
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at           android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at com.adev.timelonn.DatabaseHandler.addDay(DatabaseHandler.java:79)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at com.adev.timelonn.AddHours.onClick(AddHours.java:99)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.view.View.performClick(View.java:4084)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.view.View$PerformClick.run(View.java:16966)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.os.Handler.handleCallback(Handler.java:615)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.os.Looper.loop(Looper.java:137)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at android.app.ActivityThread.main(ActivityThread.java:4931)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at java.lang.reflect.Method.invoke(Method.java:511)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-27 23:02:51.451: E/SQLiteDatabase(6777):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-27 23:02:51.451: E/SQLiteDatabase(6777): at dalvik.system.NativeStart.main(Native   
Method)

My database file.

 public class DatabaseHandler extends SQLiteOpenHelper {
 private static final int DATABASE_VERSION = 1;

 // Database Name
 private static final String DATABASE_NAME = "timeliste";

 // Contacts table name
 private static final String TABLE_DAYS = "dager";

 // Contacts Table Columns names
 private static final String KEY_ID = "id";
 private static final String KEY_DATE = "date";
 private static final String KEY_HOURS = "hours";
 private static final String KEY_UB = "ub";
 private static final String KEY_BRUTTO = "brutto";

 public DatabaseHandler(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 // Creating Tables
 @Override
  public void onCreate(SQLiteDatabase db)        
 String CREATE_DAY_TABLE = "CREATE TABLE " + TABLE_DAYS + "(" 
 + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT," + KEY_HOURS + " INTEGER,     
     " 
 + KEY_UB + " INTEGER," + KEY_BRUTTO + "INTEGER," + ")"; 
 db.execSQL(CREATE_DAY_TABLE);
 }

 // Upgrading database
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // Drop older table if existed
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAYS);

  // Create tables again
  onCreate(db);
 }

 /**
  * All CRUD(Create, Read, Update, Delete) Operations
  */

 // Adding new contact
 void addDay(AddNewDay newday) {
  SQLiteDatabase db = this.getWritableDatabase();


  ContentValues values = new ContentValues();
  values.put(KEY_DATE, newday.getDate()); // GetDate
  values.put(KEY_BRUTTO, newday.getBrutto()); // GetBruttoLønn
  values.put(KEY_HOURS, newday.getHours()); // GetHours
  values.put(KEY_UB, newday.getUb()); // GetUBTillegg

  // Inserting Row
  db.insert(TABLE_DAYS, null, values);
  db.close(); // Closing database connection
 }

 // Get single day
 AddNewDay getContact(int id) {
  SQLiteDatabase db = this.getReadableDatabase();

  Cursor cursor = db.query(TABLE_DAYS, new String[] { KEY_ID,
    KEY_DATE, KEY_HOURS, KEY_BRUTTO, KEY_UB }, KEY_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null, null);
  if (cursor != null)
   cursor.moveToFirst();

  AddNewDay newday = new AddNewDay(Integer.parseInt(cursor.getString(0)),
    cursor.getString(1), Integer.parseInt(cursor.getString(2)),
    Integer.parseInt(cursor.getString(3)),Integer.parseInt(cursor.getString(4)));
  // return contact
  return newday;
 }

 // Getting All Contacts
 public List<AddNewDay> getAllContacts() {
  List<AddNewDay> contactList = new ArrayList<AddNewDay>();
  // Select All Query
  String selectQuery = "SELECT  * FROM " + TABLE_DAYS;

  SQLiteDatabase db = this.getWritableDatabase();
  Cursor cursor = db.rawQuery(selectQuery, null);

  // looping through all rows and adding to list
  if (cursor.moveToFirst()) {
   do {
    AddNewDay days = new AddNewDay();
    days.setID(Integer.parseInt(cursor.getString(0)));
    days.setDate(cursor.getString(1));
    days.setHours(Integer.parseInt(cursor.getString(2)));
    days.setUb(Integer.parseInt(cursor.getString(3)));
    days.setBrutto(Integer.parseInt(cursor.getString(4)));
    // Adding contact to list
    contactList.add(days);
   } while (cursor.moveToNext());
  }

  // return contact list
  return contactList;
 }

 // Updating single contact
 public int updateDay(AddNewDay newday) {
  SQLiteDatabase db = this.getWritableDatabase();

  ContentValues values = new ContentValues();
  values.put(KEY_ID,  newday.getID());
  values.put(KEY_DATE, newday.getDate());
  values.put(KEY_HOURS, newday.getHours());
  values.put(KEY_UB, newday.getUb());
  values.put(KEY_BRUTTO, newday.getUb());

  // updating row
  return db.update(TABLE_DAYS, values, KEY_ID + " = ?",
    new String[] { String.valueOf(newday.getID()) });
 }

 // Deleting single contact
 public void deleteDay(AddNewDay newday) {
  SQLiteDatabase db = this.getWritableDatabase();
  db.delete(TABLE_DAYS, KEY_ID + " = ?",
    new String[] { String.valueOf(newday.getID()) });
  db.close();
 }

 // Getting contacts Count
 public int getContactsCount() {
  String countQuery = "SELECT  * FROM " + TABLE_DAYS;
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor cursor = db.rawQuery(countQuery, null);
  cursor.close();

  // return count
  return cursor.getCount();
 }


}

And then my AddNewDay file.

  public class AddNewDay {

 //private variables
     int _id;
     String _date;
     int _hours;
     int _ubtillegg;
     int _brutto;        

     // Empty constructor
     public AddNewDay(){
     }
     // constructor
     public AddNewDay(int id, String date, int hours, int ubtillegg, int            brutto){
      this._id = id;
      this._date = date;
      this._hours = hours;
      this._ubtillegg = ubtillegg;
      this._brutto = brutto;
     }       
     // constructor
     public AddNewDay(String date, int hours, int brutto){
      this._date = date;
      this._hours = hours;
      this._brutto = brutto;
     }
     // getting ID
     public int getID(){
      return this._id;
     }

     // setting id
     public void setID(int id){
      this._id = id;
     }

     // getting date
     public  String getDate(){
      return this._date;
     }

     // setting date
     public void setDate(String date){
      this._date = date;
     }

     // getting hours
     public int getHours(){
      return this._hours;
     }

     // setting hours
     public void setHours(int hours){
      this._hours = hours;
     }

     // getting ubtillegg
     public int getUb(){
      return this._ubtillegg;
     }

     // setting ubtillegg
     public void setUb(int ub){
      this._ubtillegg = ub;
     }

     // getting brutto
     public int getBrutto(){
      return this._brutto;
     }

     // setting brutto
     public void setBrutto(int brutto){
      this._brutto = brutto;
     }

             public String toString() {
         return _date + " jobbet du " + _hours + " timer.";
             }  
            }

And this is how I add a new entry in the database.

        // # Makes a new object of newday.
        AddNewDay newday = new AddNewDay ();
        newday._date = "21.03.2013";
        newday._id = 1;
        newday._hours = 4;
        newday._ubtillegg = 1500;

        // # Open databse connection and writes new day.
        DatabaseHandler connect = new DatabaseHandler (this);
        connect.addDay(newday);

        // # Updates the dblist with entries.
        GlobalVariables.dblist = connect.getAllContacts();

解决方案

So basicly I found the solution. I'm still confused about how stupid it was. And clearly database work i super-sensitive material! My problem was I forgot a white-space in the last "INTEGER" value. So instead of " INTEGER" i wrote "INTEGER", and that gave me a row called "bruttoINTEGER" instead of "brutto". So a white-space was the error. I rewrote the createTable function to this :

 public void onCreate(SQLiteDatabase db) {
   String CREATE_DAY_TABLE = "CREATE TABLE " + TABLE_DAYS + "(" 
 + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT," + KEY_HOURS + " INTEGER, " 
 + KEY_UB + " INTEGER," + KEY_BRUTTO + " INTEGER" + ");"; 
 db.execSQL(CREATE_DAY_TABLE);
 }

这篇关于安卓:表中没有指定列&QUOT;变量名&QUOT; MYSQL数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆