JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 SQLite底下無法建立Table的問題
causemx

什麼時候可以獨當一面...?



發文: 128
積分: 0
於 2010-11-26 18:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我從SQLiteOpenHelper中實作出一個類別如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  public static class DatabaseHelper extends SQLiteOpenHelper {
       
// 資料庫目標資料夾
     private static final String DATABASE_TARGET = "/sdcard/Fatnet/database/";
       
// 資料庫名稱
     private static final String DATABASE_NAME = "data.db";
       
// 資料庫完整路徑
     private static final String DATABASE_FULL_PATH = DATABASE_TARGET + DATABASE_NAME;
       
// 資料庫版本號
     private static final int DATABASE_VERSION = 1;
       
// 資料表名稱
     private static final String DATABASE_TABLE = "notes2";
       
// 資料表欄位
     private static final String DATABASE_CREATE = 
          "create table notes2("
          +"_id INTEGER PRIMARY KEY,"
          +"UserID VARCHAR,"
          +"RecTime DATETIME,"
          +");";
 
    private SQLiteDatabase myDataBase;
      
    public DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
      // TODO Auto-generated constructor stub
      File mFile = new File(DATABASE_TARGET);
            if (!mFile.exists()) mFile.mkdirs();
            try {
                this.createDataBase();
            } catch (IOException e) {
                Log.d("causeWARN", "Can't create database");
            }
    }
    
    public void createDataBase() throws IOException {
          if (!checkDataBase()) this.getWritableDatabase();
          else Log.d("causeWARN", "Check over");
      }
      
    public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
          try {
              checkDB = SQLiteDatabase.openOrCreateDatabase(DATABASE_FULL_PATH, null);
          } catch (SQLiteException e) {
              Log.d("causeWARN", "Problem of create database");
              // database does't exist yet.
          }
          if (checkDB != null) {
              checkDB.close();
          }
          return checkDB != null ? true : false;
    }
    
    public void openDB(int mode) throws SQLException {
        try {
            myDataBase = SQLiteDatabase.openDatabase(DATABASE_FULL_PATH, null, mode);
        } catch(SQLException e) {
            Log.d("causeWARN", "Database non-closed. Reopening.");
              myDataBase = SQLiteDatabase.openDatabase(DATABASE_FULL_PATH, null, mode);
        }
    }
    
    public void openDB() throws SQLException {
        openDB(SQLiteDatabase.OPEN_READWRITE);
    }
    
    public SQLiteDatabase getDB() {
        return myDataBase;
    }
    
    @Override
      public synchronized void close() {
          if (myDataBase != null)
              myDataBase.close();
          super.close();
      }
      
    @Override
    public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
        myDataBase.execSQL(DATABASE_CREATE);
              Log.d("causeWARN", "Table created");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      myDataBase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
      onCreate(myDataBase);
    }
 
    }


而操作方式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class XDBAdapter{
    private Context mCtx = null;
    private DatabaseHelper dbHelper ;
    private SQLiteDatabase db;
 
    /** Constructor */
    public XDBAdapter(Context ctx) {
        this.mCtx = ctx;
    }
 
    public XDBAdapter open () throws SQLException {
        dbHelper = new DatabaseHelper(mCtx);
        dbHelper.openDB();
        db = dbHelper.getDB();
        Log.d("causeWARN", "message: " + "DB has Establish!");
        return this;
    }
}


Insert的部份我就不放了...有點冗長,然而實際上執行的時候,onCreate()這個函式卻沒執行到,導致系統一直產生"no such table: note2" 的錯誤, 請問為什麼onCreate()函式在建構時的運作情況是如何? 照理來說建構時呼叫的話不至於產生表沒建置的情況...奇怪的是這程式在手機上可以成功建表,但是在模擬器卻無法建表導致錯誤。


causemx edited on 2010-11-26 18:17
reply to postreply to post
無招勝有招
作者 Re:SQLite底下無法建立Table的問題 [Re:causemx]
luo673





發文: 171
積分: 0
於 2010-11-27 07:05 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
:::Table名稱是:notes2還是note2 ???

reply to postreply to post
作者 Re:SQLite底下無法建立Table的問題 [Re:luo673]
causemx

什麼時候可以獨當一面...?



發文: 128
積分: 0
於 2010-11-27 10:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
notes2

reply to postreply to post
無招勝有招
作者 Re:SQLite底下無法建立Table的問題 [Re:luo673]
causemx

什麼時候可以獨當一面...?



發文: 128
積分: 0
於 2010-11-29 11:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Hey, 我解決了= ="
在進入程式的時候要先做表存在與否的檢查,然後再建表(我一直傻傻的以為Helper介面裡面的onCreate會自動執行....),所以加入這段:

1
2
3
4
Cursor myCursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='notes2'", null);
if (myCursor.getCount() == 0) {
   dbHelper.onCreate(db);
}


這樣就解決了=.=''


reply to postreply to post
無招勝有招
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8