2013-03-20 40 views
0

我使用有一個表名評論存儲ID sqlite的DATABSE開發應用和區塊SQLite數據庫錯誤:sqlite的返回:錯誤碼= 1,味精=沒有這樣的列:ph_number

現在我每次得到一個錯誤,當我運行

sqlite returned: error code = 1, msg = no such column: ph_number

的代碼如下應用..

public class MySQLiteHelper extends SQLiteOpenHelper { 

    public static final String TABLE_COMMENTS = "comments"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_COMMENT = "comment"; 
    public static final String COLUMN_NUMBER = "ph_number"; 
    public static final String COLUMN_BLOCK = "block"; 

    private static final String DATABASE_NAME = "commments.db"; 
    private static final int DATABASE_VERSION = 1; 

    // Database creation sql statement 
    private static final String DATABASE_CREATE = "create table " 
     + TABLE_COMMENTS + "(" + COLUMN_ID 
     + " integer primary key autoincrement, " + COLUMN_COMMENT 
     + " TEXT," + COLUMN_NUMBER + " TEXT," 
     + COLUMN_BLOCK +" TEXT," +"UNIQUE(COLUMN_NUMBER) ON CONFLICT REPLACE"+ ");"; 

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

    @Override 
    public void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w(MySQLiteHelper.class.getName(), 
     "Upgrading database from version " + oldVersion + " to " 
      + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS); 
    onCreate(db); 
    } 

} 

這是第二類代碼..

public class CommentsDataSource { 

    // Database fields 
    private SQLiteDatabase database; 
    private MySQLiteHelper dbHelper; 
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
     MySQLiteHelper.COLUMN_COMMENT, 
     MySQLiteHelper.COLUMN_NUMBER, 
     MySQLiteHelper.COLUMN_BLOCK}; 
    private String[] ColNum = {MySQLiteHelper.COLUMN_NUMBER, MySQLiteHelper.COLUMN_BLOCK }; 
    private String[] num = {MySQLiteHelper.COLUMN_NUMBER }; 

    public CommentsDataSource(Context context) { 
    dbHelper = new MySQLiteHelper(context); 
    } 
    public CommentsDataSource() { 
     //dbHelper = new MySQLiteHelper(context); 
     } 

    public void open() throws SQLException { 
    database = dbHelper.getWritableDatabase(); 
    } 

    public void close() { 
    dbHelper.close(); 
    } 

    public Comment createComment(String comment, String number, String b_value) { 
    ContentValues values = new ContentValues(); 
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment); 
    values.put(MySQLiteHelper.COLUMN_NUMBER, number); 
    values.put(MySQLiteHelper.COLUMN_BLOCK, b_value); 
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null, 
     values); 
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
     allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null, 
     null, null, null); 
    cursor.moveToFirst(); 
    Comment newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
    } 

    public void deleteComment(Comment comment) { 
    long id = comment.getId(); 
    System.out.println("Comment deleted with id: " + id); 
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID 
     + " = " + id, null); 
    } 

    public List<Comment> getAllComments() { 
     List<Comment> comments = new ArrayList<Comment>(); 
     try{ 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
      allColumns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      Comment comment = cursorToComment(cursor); 
      comments.add(comment); 
      cursor.moveToNext(); 
     } 
     // Make sure to close the cursor 
     cursor.close(); 
     }catch (Exception e) { 
      // TODO: handle exception 
     } 

     return comments; 
     } 
// Updating single contact 
    public Comment updateContact(Comment comment, String name ,String number, String b_value) { 
     database = dbHelper.getWritableDatabase(); 
     long id = comment.getId(); 
     ContentValues values = new ContentValues(); 
     values.put(MySQLiteHelper.COLUMN_COMMENT, name); 
     values.put(MySQLiteHelper.COLUMN_NUMBER, number); 
     values.put(MySQLiteHelper.COLUMN_BLOCK, b_value); 
     System.out.println("updated: "+ id); 
     // updating row 
     long updateId = database.update(MySQLiteHelper.TABLE_COMMENTS, values, 
       MySQLiteHelper.COLUMN_ID + " = " + id, null); 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
       allColumns, MySQLiteHelper.COLUMN_ID + " = " + id, null, 
       null, null, null); 
      cursor.moveToFirst(); 
      Comment newComment = cursorToComment(cursor); 
      cursor.close(); 
      System.out.println("updated: "+ newComment); 
      return newComment; 
    } 

    private Comment cursorToComment(Cursor cursor) { 
    Comment comment = new Comment(); 
    comment.setId(cursor.getLong(0)); 
    comment.setName(cursor.getString(1)); 
    comment.setNumber(cursor.getString(2)); 
    comment.setB_value(cursor.getString(3)); 
    return comment; 
    } 

public String getValue(String phNum) { 
     String value = null; 
     String b_value =null ; 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
      ColNum, null, null, null, null, null); 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      value = cursor.getString(0); 
      if(PhoneNumberUtils.compare(phNum, value)) 
      { 
       b_value = cursor.getString(1); 
      } 
      cursor.moveToNext(); 
     } 
     // Make sure to close the cursor 
     cursor.close(); 
     return b_value; 
     } 
public boolean findNum(String phNum) { 
     String value = null; 
     boolean find =false ; 
     Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, 
      num, null, null, null, null, null); 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      value = cursor.getString(0); 
      if(PhoneNumberUtils.compare(phNum, value)) 
      { 
       find = true; 
      } 
      cursor.moveToNext(); 
     } 
     // Make sure to close the cursor 
     cursor.close(); 
     return find; 
     } 
} 

我已經宣佈列ph_number爲。

public static final String COLUMN_NUMBER = "ph_number"; 

,但我還是我得到的錯誤sqlite returned: error code = 1, msg = no such column: ph_number

我對這個浪費時間很多,但不能糾正。請幫助我..

回答

1

試試這個

// Database creation sql statement 
    private static final String DATABASE_CREATE = "create table " 
    + TABLE_COMMENTS + "(" + COLUMN_ID 
    + " integer primary key autoincrement, " + COLUMN_COMMENT 
    + " TEXT," + COLUMN_NUMBER + " TEXT," 
    + COLUMN_BLOCK +" TEXT," +"UNIQUE("+ COLUMN_NUMBER +") ON CONFLICT REPLACE"+ ");"; 

,而不是

// Database creation sql statement 
    private static final String DATABASE_CREATE = "create table " 
    + TABLE_COMMENTS + "(" + COLUMN_ID 
    + " integer primary key autoincrement, " + COLUMN_COMMENT 
    + " TEXT," + COLUMN_NUMBER + " TEXT," 
    + COLUMN_BLOCK +" TEXT," +"UNIQUE(COLUMN_NUMBER) ON CONFLICT REPLACE"+ ");"; 
+0

謝謝@Divya ..它解決了我的問題.. – 2013-03-21 05:17:29

0

我認爲錯誤的SQLite返回:錯誤碼= 1,味精=沒有這樣的列:ph_number是因爲,

在你的數據庫代碼 公共靜態最後絃樂COLUMN_NUMBER欄=「ph_number」 ; 包含列名稱ph_number。

根據我的理解,您聲明數據庫包含存儲id,名稱,編號和塊的一個表名稱註釋。所以可能是你的列名是數字。

所以改變這條線像這樣 public static final String COLUMN_NUMBER =「number」 現在檢查出來。因爲表格和程序中的列應該相同。所以可能你會得到沒有這樣的列錯誤

+0

仍然給錯誤.. SQLite的返回:錯誤碼= 1,味精=沒有這樣的列: number – 2013-03-20 06:19:13

+0

檢查數據庫中的列名和public static final String COLUMN_NUMBER =「number」是否正確。數據庫不區分大小寫。你會得到這個錯誤,因爲你的列名稱在數據庫和代碼,如果它不匹配..檢查出第一個 – user1835052 2013-03-20 06:25:05

+0

是的,它是相同的沒有區別列名 – 2013-03-20 06:27:44

相關問題