" table *没有名为*&#34的列;错误,SQLite

时间:2013-03-05 17:40:00

标签: java android sqlite

我正在尝试学习SQLite,尝试基本的东西和(正如预期的那样)它并没有真正起作用。

我尝试创建另一个DB(不同的文件),但错误仍然相同。

03-05 17:20:18.989: D/myLogs(1323): --- Insert in mytable: ---
03-05 17:20:18.999: E/SQLiteLog(1323): (1) table mytable has no column named date
03-05 17:20:19.199: E/SQLiteDatabase(1323): Error inserting time=asd date=asd glucose=adf
03-05 17:20:19.199: E/SQLiteDatabase(1323): android.database.sqlite.SQLiteException: table mytable has no column named date (code 1): , while compiling: INSERT INTO mytable(time,date,glucose) VALUES (?,?,?)

这里是LogCat输出:http://s15.postimage.org/soak9ifbf/androidlog.png(无法添加图片,必须为您提供链接)

基本上,它说,没有名为“date”的列

以下是代码:

package com.example.prototype2;



import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
 import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;

 public class AddGlucose extends Activity implements OnClickListener {

final String LOG_TAG = "myLogs";

Button btnAdd, btnRead, btnClear;
  EditText etGlucose, etTime, etDate;

  DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_glucose);
    // Show the Up button in the action bar.
    setupActionBar();

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);

    btnRead = (Button) findViewById(R.id.btnRead);
    btnRead.setOnClickListener(this);

    btnClear = (Button) findViewById(R.id.btnClear);
    btnClear.setOnClickListener(this);

    etGlucose = (EditText) findViewById(R.id.etGlucose);
    etTime = (EditText) findViewById(R.id.etTime);
    etDate = (EditText) findViewById(R.id.etDate);
    dbHelper = new DBHelper(this);
}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.add_glucose, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    String glucose = etGlucose.getText().toString();
    String time = etTime.getText().toString();
    String date = etDate.getText().toString();

    SQLiteDatabase db = dbHelper.getWritableDatabase();

    switch (v.getId()) {
    case R.id.btnAdd:
      Log.d(LOG_TAG, "--- Insert in mytable: ---");

      cv.put("glucose", glucose);
      cv.put("time", time);
      cv.put("date", date);
    long rowID = db.insert("mytable", null, cv);
      Log.d(LOG_TAG, "row inserted, ID = " + rowID);
      break;

    case R.id.btnRead:
        Log.d(LOG_TAG, "--- Rows in mytable: ---");
        Cursor c = db.query("mytable", null, null, null, null, null, null, null);
        if (c.moveToFirst()) {
            int idColIndex = c.getColumnIndex("id");
            int glucoseColIndex = c.getColumnIndex("glucose");
            int timeColIndex = c.getColumnIndex("time");
            int dateColIndex = c.getColumnIndex("date");

            do {    

                Log.d(LOG_TAG,
                        "ID = " + c.getInt(idColIndex) + 
                        ", glucose = " + c.getString(glucoseColIndex) +
                        ", time = " + c.getString(timeColIndex) + ", date = " + c.getString(dateColIndex));
            } while (c.moveToNext());
        } else
            Log.d(LOG_TAG, "0 rows");
        c.close();
        break;

case R.id.btnClear:
      Log.d(LOG_TAG, "--- Clear mytable: ---");

      int clearCount = db.delete("mytable", null, null);
      Log.d(LOG_TAG, "deleted rows count = " + clearCount);
      break;
    }
    dbHelper.close();
  }

 class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {

          super(context, "mytable", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
          Log.d(LOG_TAG, "--- onCreate database ---");
          // создаем таблицу с полями
          db.execSQL("create table mytable ("
              + "id integer primary key autoincrement," 
              + "glucose text,"
              + "time text" + "date text" + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
      }

}

4 个答案:

答案 0 :(得分:5)

错误只是说表mytable不包含请求的列date。除此之外没有魔法。

您应该再次删除并创建表,并确保它具有正确的列。请注意,您的onCreate方法包含错误。 ,后遗失了time text。所以基本上你的数据库表甚至都没有在这个方法中创建,因为SQL是无效的。

尝试使用以下代码在onCreate()中创建表格:

      db.execSQL("create table mytable ("
          + "id integer primary key autoincrement," 
          + "glucose text,"
          + "time text," // added a ','
          + "date text" + ");");

答案 1 :(得分:0)

只添加一个,会使得create table sql无法创建表,因此它使用的是表的先前版本

答案 2 :(得分:0)

当我100%确定我的表包含该列时,我遇到了这个错误。经过几个小时的尝试我可以,我检查了表,发现在更改我的代码时我仍然更改了旧列名这些列名称与我的代码中的列名称不匹配。我在我的设备上卸载了应用程序并重新安装,问题解决了。

答案 3 :(得分:0)

检查您的列是否符合该法术并插入查询中。 还要检查您的查询冒号和逗号(,)和(")是否已正确插入。