【问题标题】:SQLite query SQLiteLog (1) no such Column:SQLite查询SQLiteLog(1)没有这样的Column:
【发布时间】:2012-10-22 12:26:07
【问题描述】:

我有 3 节课。 一个设置数据库和表,工作正常。 DBHelper.java

private static final String TAG = "DBHelper";
public static final String DB_NAME = "pat_test.db";
public static final String TABLE5 = "sites";
public static final String S_ID = BaseColumns._ID;
public static final String S_CLIENT = "client_of_site";
public static final String S_POSTCODE = "site_postode";
public static final String S_CON_NAME = "contact_name";
public static final String S_CON_NUM = "contact_tel_num";
public static final String S_NAME = "site_name";
public static final String S_LAST_TEST = "last_test_date";

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {


String sql5 = String.format("create table %s (%s INT PRIMARY KEY, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INT, %s TEXT)",TABLE5, S_ID, S_CLIENT, S_NAME, S_POSTCODE, S_CON_NAME,S_CON_NUM,S_LAST_TEST); 
Log.d(TAG, "onCreate: " + sql5);
db.execSQL(sql5);
}

下一个有我的功能DBControl.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;    
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.provider.BaseColumns;

public class DBControl {

public static final String TABLE5 = "sites";

public static final String S_ID = BaseColumns._ID;
public static final String S_CLIENT = "client_of_site";
public static final String S_NAME = "site_name";
public static final String S_POSTCODE = "site_postode";
public static final String S_CON_NAME = "contact_name";
public static final String S_CON_NUM = "contact_tel_num";
public static final String S_LAST_TEST = "last_test_date";

private Context context;
private SQLiteDatabase database;
private DBHelper dbhelper;

public DBControl(Context context) {

    this.context = context;

}

  public DBControl open() throws SQLiteException {

    dbhelper = new DBHelper(context);
    database = dbhelper.getWritableDatabase();
    return this;
}

public void close() {

    dbhelper.close();

}
   public ContentValues createContentValues3(String CLIENT, 
        String NAME,    String POSTCODE, String CON_NAME, 
         long CON_NUM) {

    ContentValues values = new ContentValues();
    values.put(S_CLIENT, CLIENT);
    values.put(S_POSTCODE, POSTCODE);
    values.put(S_CON_NAME, CON_NAME);
    values.put(S_CON_NUM, CON_NUM);
    values.put(S_NAME, NAME);
    return values;
  }  
   public long addSiteDetails( String CLIENT, String NAME,
        String POSTCODE, String CON_NAME, int CON_NUM ) {

    ContentValues siteValues = createContentValues3( CLIENT, NAME,
            POSTCODE, CON_NAME, CON_NUM );

    return database.insert(TABLE5, null, siteValues);
}


public boolean updateSiteDetails(long id, String CLIENT, String NAME,
        String POSTCODE, String CON_NAME , int CON_NUM
        ) {

    ContentValues siteUpdateValues = createContentValues3( CLIENT, NAME,
            POSTCODE, CON_NAME, CON_NUM);

    return database.update(TABLE5, siteUpdateValues, S_ID + "=" + id, null) > 0;
}

}
  public long fetchSiteIdByName(String NAME){

     Cursor dbCursor; 
     long id = 0;

      try { 

     dbCursor = database.query(true, TABLE5, new String []{S_ID}, S_NAME + "= " + NAME  , null, null, null, null, null);
     dbCursor.moveToFirst(); 
     id = dbCursor.getLong(dbCursor.getColumnIndex(S_ID)); } 

      catch (SQLiteException e) { 

          id = -1;

      }

      return id;

      }

我的第三节课是用来打电话检查的。但这是我的问题出现的地方,或者在上一课的 fetchSiteIdByName 中。

  import android.app.Activity;
  import android.app.Dialog;
  import android.content.Intent;
  import android.database.sqlite.SQLiteException;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.AutoCompleteTextView;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;

  public class activityStartNewTest extends Activity implements OnClickListener {

Button buttonBeginTesting;
private AutoCompleteTextView clientInput;       //Refers to input box for client
private AutoCompleteTextView siteInput;         //Refers to input box for site
private AutoCompleteTextView postcodeInput;     //Refers to input box for postcode
private EditText nameInput;                     //Refers to input box for contact name
private EditText telInput;                      //Refers to input box for contact number
  //    private EditText jobrefInput;                   //Refers to input box for Job reference number

private DBControl dbControl;                

@ Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_new_test);
    dbControl = new DBControl(this);
    clientInput = (AutoCompleteTextView) findViewById(R.id.autoCompleteClientNameEdit);
    siteInput = (AutoCompleteTextView) findViewById(R.id.autoCompleteSiteNameEdit);
    postcodeInput = (AutoCompleteTextView) findViewById(R.id.autoCompletePostcodeEdit);
    nameInput = (EditText) findViewById(R.id.editTextContactName);
    telInput = (EditText) findViewById(R.id.editTextContactNum);
  //        jobrefInput = (EditText)findViewById(R.id.editTextJobRef);      

    buttonBeginTesting = (Button) findViewById(R.id.buttonBeginTesting);
    buttonBeginTesting.setOnClickListener(this);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public void onClick(View arg) {

    String clientData = clientInput.getText().toString();
    String siteData = siteInput.getText().toString();
    String postcodeData = postcodeInput.getText().toString();
    String nameData = nameInput.getText().toString();
    String telData = telInput.getText().toString();
     // String jobrefData = jobrefInput.getText().toString();

    Toast toastNotice = new Toast(this);
    Dialog notice = new Dialog(this); // this needs the textview below for displaying
    TextView msgBody = new TextView(this);
    msgBody.setTextSize(20);
    long tempValue = 0;

    switch(arg.getId()){

    case R.id.buttonBeginTesting:

    try{
        int telDataAsNum = Integer.parseInt(telData);
        dbControl.open();

        if((tempValue = dbControl.fetchSiteIdByName(siteData)) != -1) {

            if(dbControl.updateSiteDetails(tempValue, clientData, siteData, postcodeData, nameData, telDataAsNum)){
                notice.setTitle("Site Updated!");
                msgBody.setText("Site detail have been updated instead");
            }
            else{
                notice.setTitle("Update failed!");
                msgBody.setText("Site already Exists, but failed!");
            }
        }
        else{
            long siteID = 0;

            siteID = dbControl.addSiteDetails( clientData, siteData, postcodeData, nameData, telDataAsNum );
            notice.setTitle("Site Added");
            msgBody.setText("Site added at row "+ siteID);
    }
    dbControl.close();

    }
    catch (SQLiteException e){ // Notifies user if SQL error occurred
        e.printStackTrace();
        notice.setTitle("Insert failed");
        msgBody.setText("SQL Error!");
    }
    catch (NumberFormatException e){ // Notifies user if NUM format error occurred
        e.printStackTrace();
        notice.setTitle("Insert failed");
        msgBody.setText("Contact number must be a number!");
    }
    notice.setContentView(msgBody);
    notice.show();
  }
  }
  }

现在,当我运行应用程序时,我将新的详细信息插入到数据库中,绝对没问题。 使用 adb 我拉数据库并在 sqlite3 中打开。它显示第一列中为 NULL 且最后一列为 NULL 的所有条目(最后一个尚未发布)。 我在编辑框 siteData 的字段中使用了相同的文本,它只是添加了一个新条目,并没有更新以前的条目。

日志猫显示 SQLiteLog (1) no such column: xxx 其中 xxx 将是进入 autocompleteTextView 的条目。

为什么它要查找以输入的任何文本命名的列?

【问题讨论】:

    标签: android sqlite android-cursor


    【解决方案1】:

    用单引号将 fetchSiteIdByName() 中的 NAME 括起来可能会有所帮助。

    dbCursor = database.query(true, TABLE5, new String []{S_ID}, S_NAME + "= '" + NAME + "'"  , null, null, null, null, null);
    

    我不太确定它是否可以在 SQLite 上运行,但我在 MySQL 中遇到过同样的问题,它会有所帮助。此外,当您在数据库中获取非数字值时,您将它们括在单引号中。

    希望对您有所帮助。 :)

    【讨论】:

    • 我尝试添加单引号和双引号,单引号,双引号,但它返回了在最后一个类中捕获的错误 else{ notice.setTitle("Update failed!"); msgBody.setText("站点已经存在,但是失败了!");当它是重复条目时,在 logcat 中出现与上述相同的错误“没有这样的列:然后刚刚崩溃了一个新条目?E / AndroidRuntime(3835):android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0
    • 是的,我认为您应该检查一下,因为它可能会有所帮助stackoverflow.com/questions/5316336/…
    【解决方案2】:

    我似乎遇到了 3 个不同的问题,但使用 Lokesh 的链接和帖子可以帮助我解决问题。

    我将DBelper.java中的SQL字符串修改为这个

       String sql5 = String.format("create table %s (%s INTEGER PRIMARY KEY **AUTOINCREMENT**, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INT, %s TEXT)",
                        TABLE5, S_ID, S_CLIENT, S_NAME, S_POSTCODE, S_CON_NAME,
                        S_CON_NUM, S_LAST_TEST);
    

    并添加到 DBControl.java 类中

      **if**(dbCursor.moveToFirst()) id = dbCursor.getLong(dbCursor.getColumnIndex(S_ID));  
    

    在 moveToFirst()) 之后删除分号。就像我说的我是一个真正的菜鸟,但相信这是正确的。

    当我卸载并重新安装应用程序时,添加 AUTOINCREMENT 有 1 个显着效果(我可以更改数据库版本,如果我也想的话,我知道)。

    重建表时,它们没有我以前见过的列 _id。所以我认为 SQL 字符串是我失败的原因。我确定我在某处听说 PRIMARY KEY basecolumns._ID 不需要它,但现在我知道它是。

    感谢 Lokesh 的指点。如果没有一些指导,我无法找到这个问题,我真的很感激。

    我尝试添加到向上箭头,但还没有足够的评分。呵呵。

    希望这对其他人有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多