2011-12-28 44 views
1

我做了記事本教程,我的SQL服務器是一個修改版本。出於某種原因,我的應用沒有正確寫入。任何幫助將不勝感激。我不明白哪部分代碼是錯誤的。爲什麼我的SQL服務器不能正常工作(android SDK)?

package com.drawing; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class NotesDbAdapter { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_X = "x"; 
public static final String KEY_Y = "y"; 
public static final String KEY_Size = "size"; 
private static final String TAG = "NotesDbAdapter"; 
private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb; 
private static final String DATABASE_NAME = "data"; 
private static final String DATABASE_TABLE = "notes"; 
private static final int DATABASE_VERSION = 2; 
private static final String DATABASE_CREATE = 
      " create table " + DATABASE_TABLE + " (" 
     + KEY_ROWID + " integer primary key autoincrement, " 
     + KEY_X + " text not null, " 
     + KEY_Y + " text not null, " 
     + KEY_Size + " text not null);"; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

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


      @Override 
      public void onCreate(SQLiteDatabase db) { 
       try { 
        db.execSQL(DATABASE_CREATE); 
       } catch (Exception e) { 
        Log.e("dbAdapter", e.getMessage().toString()); 
       } 
      } 

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

    } 

    public NotesDbAdapter(Context ctx) { 
     this.mCtx = ctx; 
    } 

    public NotesDbAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

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


    public long createNote(float f, float g, int size) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_X, f); 
     initialValues.put(KEY_Y, g); 
     initialValues.put(KEY_Size, size); 
     return mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public boolean deleteNote(long rowId) { 

     return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
    } 


    public Cursor fetchAllNotes() { 

     return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_X,KEY_Y,KEY_Size}, null, null, null, null, null); 
    } 

    public Cursor fetchNote(long rowId) throws SQLException { 

     Cursor mCursor = 

      mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_X,KEY_Y,KEY_Size}, KEY_ROWID + "=" + rowId, null, 
        null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 

    } 

    public boolean updateNote(long rowId, int x, int y, int size) { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_X, x); 
     args.put(KEY_Y, y); 
     args.put(KEY_Size, size); 
     return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 
+0

您是否收到錯誤消息,代碼是否失敗或...? – 2011-12-28 22:40:00

+0

它說「錯誤插入表'筆記'」。謝謝參觀。 – jersam515 2011-12-28 22:55:46

+0

是否可以看到爲此生成的INSERT?你確定桌子是創建的嗎?是否有其他電話 - 即。 SELECT工作? – 2011-12-28 22:59:55

回答

3

我覺得你的問題是從這裏開始:

public static final String KEY_ROWID = "_id";  
public static final String KEY_X = "_id";  
public static final String KEY_Y = "_id";  
public static final String KEY_Size = "_id"; 

我猜應該是更像我鏈接到一個問題:

public static final String KEY_BOOK = "book"; 
public static final String KEY_AUTHOR = "author"; 
public static final String KEY_ISBN = "isbn"; 
public static final String KEY_RATING = "rating"; 
public static final String KEY_ROWID = "_id"; 

其次,你的表的創建也似乎是錯的:

private static final String DATABASE_CREATE = 
      " create table " + DATABASE_TABLE + " (" 
        + KEY_ROWID + " integer primary key autoincrement, " 
        + KEY_X + " integer primary key autoincrement, " 
        + KEY_Y + " integer primary key autoincrement, " 
        + KEY_Size + " integer primary key autoincrement, "; 

你已經創建了所有的字段作爲主鍵和自動增量 - 這沒有任何意義。再看另一個問題:

private static final String DATABASE_CREATE = 
     " create table " + DATABASE_TABLE + " (" 
    + KEY_ROWID + " integer primary key autoincrement, " 
    + KEY_AUTHOR + " text not null, " 
    + KEY_BOOK + " text not null, " 
    + KEY_ISBN + " text not null, " 
    + KEY_RATING + " text not null);"; 

這樣做更有意義。

請仔細看看下面的問題 - 並重新閱讀您開始使用的教程。

I keep getting the SQLException error with this

這裏的問題可能有問題,但似乎可以正確識別列(這是你似乎有問題)。答案會經過其他一些步驟 - 我建議您在自己的問題的右側查看「相關」建議。你可能會發現更多的指針來引導你朝着正確的方向前進。

+0

感謝您的全力幫助,我將其視爲其中的一部分,但我仍然遇到同樣的錯誤。 OP – jersam515 2011-12-29 00:09:56

+0

中的新代碼在問題中插入代碼,如果覺得更合適,請覆蓋舊代碼。 – 2011-12-29 00:11:23

+0

它在那裏,對不起,我一開始有麻煩,忘了我不得不突出顯示ctrl-k。 – jersam515 2011-12-29 00:17:20

0

任何人都有同樣的問題:你需要重新初始化上面顯示的Anders列,並更改數據庫版本,以便升級運行,謝謝大家。

+0

很高興你得到它的工作。到目前爲止,已經離開,無法回覆。 – 2011-12-29 19:45:50

+0

非常感謝您給予的所有幫助。 – jersam515 2011-12-29 19:53:03

相關問題