2013-03-05 53 views
0

我試圖學習SQLite,嘗試基本的東西和(如預期的)它並沒有真正的工作。「表*沒有列命名*」錯誤,SQLite

我試着創建另一個數據庫(不同的文件),但錯誤保持不變。

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(不能添加圖片,得給你一個鏈接)

基本上,它說,不存在一個名爲「日期」

列下面是代碼:

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

     } 
     } 

} 
+1

請將logcat輸出作爲文本發佈到您的問題中 – harpun 2013-03-05 17:55:45

+0

03-05 17:20:18.989 :D/myLogs(1323):--- Insert in mytable:--- 03-05 17:20:18.999:E/SQLiteLog(1323):(1)table mytable沒有列名爲date 03-05 17 :20:19.199:E/SQLiteDatabase(1323):插入時間錯誤= asd date = asd glucose = adf 03-05 17:20:19.199:E/SQLiteDatabase(1323):android.database.sqlite.SQLiteException:table mytable沒有列名爲日期(代碼1):,編譯時:INSERT INTO mytable(時間,日期,葡萄糖)VALUES(?,?,?) – 2013-03-05 17:58:18

回答

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" + ");"); 
+0

羞恥對我來說,我正在看它的時代,並沒有注意到 – 2013-03-05 18:02:56

+0

我愛你,whoe你是誰。你是我的救生員 – 2013-03-05 18:03:43

+0

@MikhailStepanov歡迎:) – harpun 2013-03-05 18:11:09

0

只有加入了,會做的伎倆create table SQL無法創建一個表,以便它使用該表的以前版本

0

當我100%確定我的表包含該列時,發生此錯誤。經過數小時的嘗試後,我可以檢查表並發現在更改我的代碼時仍然有舊列名稱發生更改,並且這些列名稱與我的代碼中的列名稱不匹配。我卸載了我的設備上的應用程序,然後重新安裝,問題就解決了。

0

檢查您的列是否匹配拼寫並插入到查詢中。 此外,請檢查您的查詢冒號和逗號(,)和(「)是否正確插入

相關問題