0

I'm trying to learn SQLite, trying basic things and (as expected) it doesn't really work.

I've tried to create another DB (different file), but the error remains the same.

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 (?,?,?)

here is the LogCat output: http://s15.postimage.org/soak9ifbf/androidlog.png (can't add images, have to give you a link)

Basically, it says, that there is no column named "date"

Here is the code:

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) {

        }
      }

}
harpun
  • 4,022
  • 1
  • 36
  • 40
  • 1
    Please post the logcat output as text into your question. There is no need for images here. – harpun Mar 05 '13 at 17:55
  • 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 (?,?,?) – Mikhail Stepanov Mar 05 '13 at 17:58
  • You can find this answer helpful: [This can happen when you are trying to insert the data into the table where the column name does not exist or maybe schema that you are trying to insert into has been changed with the latest sequelize.js code.](https://stackoverflow.com/a/57294234/3202440) – kavigun Jul 31 '19 at 15:32

4 Answers4

6

The error just says, that the table mytable does not contain the requested column date. No magic beyond this.

You should drop and create the table again and ensure, that it has the right columns. Please note that your onCreate Method contains an error. A , is missing after time text. So basically your DB table is not even created in this method as the SQL is invalid.

Try creating the table in onCreate() with the following code:

      db.execSQL("create table mytable ("
          + "id integer primary key autoincrement," 
          + "glucose text,"
          + "time text," // added a ','
          + "date text" + ");");
harpun
  • 4,022
  • 1
  • 36
  • 40
0

Only adding a , would do the trick create table sql failed to create a table so it is using the previous version of the table

ckpepper02
  • 3,297
  • 5
  • 29
  • 43
Bilal Naqvi
  • 140
  • 1
  • 11
0

I was having this error when I was 100% certain my table contained that column.After hours of trying EVERYTHING I could, I checked the table and discovered it still had the old column names I changed while making changes to my code and these column names didn't match the column names in my code.I uninstalled the app on my device and reinstalled and the problem was solved.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
0

check your columns match the spell and are inserted in the query. Also check your query colons and commas (,) and (") are properly inserted.

PaladiN
  • 4,625
  • 8
  • 41
  • 66
Ustaad UST
  • 215
  • 2
  • 2