綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] SQLite的用法

[複製連結] 查看: 1819|回覆: 7|好評: 1
跳轉到指定樓層
樓主
e79918 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2014-5-2 16:30

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
想請問各位高手們~目前在程式中有個layout的資料以SQLite的資料庫紀錄,若是有新的layout也需要儲存資訊的話

請問是要在同個SitesDBHlp中做還是要另外做一個呢?

還是有其他可以做另一個layout的資訊儲存呢?

資料庫的地方還不是很熟...麻煩知道的高手幫忙解答  謝謝!!
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-2 17:28

回帖獎勵 +20

哈囉
SQLite
有點類似資料庫
所以你可以叫用他並建立多個資料表
不一定要分開寫
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-2 18:17
類似這樣
然後根據你要存的資料表去呼叫
A Layout 要存到A表
就呼叫相關的Create、Insert....等的副程式

  1. public class DbHleper extends SQLiteOpenHelper {
  2.         private static final String DATABASE_NAME = "ADB";
  3.         private final static int DATABASE_VERSION = 1;

  4.         public DbUtil(Context context) {
  5.                 super(context, DATABASE_NAME, null, DATABASE_VERSION);
  6.         }

  7.        
  8.         @Override
  9.         public void onCreate(SQLiteDatabase db)
  10.         {               
  11.         }

  12.         @Override
  13.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  14.                 String sql = "drop table is exists AInfo";
  15.                 db.execSQL(sql);
  16.                 onCreate(db);
  17.         }

  18. // ************** A methods ***********************


  19.         public void createAInfo() {
  20.                
  21.         }
  22.         public void dropAInfo() {
  23.                
  24.         }

  25.         public void deleteAInfo() {
  26.                
  27.         }

  28.         public void deleteAInfoOfAId(int id) {
  29.                 SQLiteDatabase db = this.getWritableDatabase();
  30.                 String where = "KeyId = ?";
  31.                 String[] whereValue = { Integer.toString(id) };
  32.                 db.delete("AInfo", where, whereValue);       
  33.                
  34.         }

  35.         public String insertAInfo(String A1,String A2, String A3,String A4, String A5) {
  36.                 SQLiteDatabase db = this.getWritableDatabase();
  37.                 ContentValues contentValue = new ContentValues();
  38.                
  39.                 contentValue.put("A1", A1);
  40.                 contentValue.put("A2", A2);
  41.                 contentValue.put("A3", A3);
  42.                 contentValue.put("A4", A4);
  43.                 contentValue.put("A5", A5);               

  44.                 try {
  45.                         long rowIndex = db.insert("AInfo", null, contentValue);
  46.                         return String.valueOf(rowIndex);
  47.                 } catch (Exception e) {
  48.                         return e.toString();
  49.                 }
  50.         }

  51.         public Cursor selectAInfo() {
  52.                 SQLiteDatabase db = this.getReadableDatabase();
  53.                 Cursor cursor = db
  54.                                 .query("AInfo", null, null, null, null, null, null);
  55.                 return cursor;
  56.         }

  57. // **************End A methods ***********************

  58. // ************** B methods ***********************
  59.        
  60.         public void createBInfo() {
  61.                 try {
  62.                         SQLiteDatabase db = this.getWritableDatabase();
  63.                         String sql = "create table if not exists BInfo (B1 varchar(32),B2 varchar(32),B3 varchar(32),B4 varchar(32))";
  64.                         db.execSQL(sql);
  65.                 } catch (Exception e) {
  66.                         e.printStackTrace();
  67.                 }
  68.         }

  69.         public void dropBInfoTable() {
  70.                  
  71.         }

  72.         public String insertBInfo(String B1,String B2, String B3,String B4) {
  73.                 SQLiteDatabase db = this.getWritableDatabase();
  74.                 ContentValues contentValue = new ContentValues();
  75.                 contentValue.put("B1", B1);
  76.                 contentValue.put("B2", B2);
  77.                 contentValue.put("B3", B3);
  78.                 contentValue.put("B4", B4);
  79.                 try {
  80.                         long rowIndex = db.insert("BInfo", null, contentValue);
  81.                         return String.valueOf(rowIndex);
  82.                 } catch (Exception e) {
  83.                         return e.toString();
  84.                 }
  85.         }

  86.        
  87.         public String selectBInfo_B1 (String B2){
  88.                 SQLiteDatabase db = this.getWritableDatabase();
  89.                 String BInfo_B1 ="";
  90.                 if(B2.length()>0)
  91.                 {
  92.                         Cursor cursor = db.query("BInfo", new String[]{"B1"}, " B2=?",new String[]{BName}, null, null, null);
  93.                         if(cursor.getCount()>0)
  94.                         {
  95.                         cursor.moveToFirst();
  96.                         BInfo_B1 =cursor.getString(cursor.getColumnIndex("B1"));
  97.                         }
  98.                 }               
  99.                
  100.                 return BInfo_B1;
  101.         }
  102.         // **************End B methods ***********************
  103.        

  104. }
複製代碼

評分

參與人數 1碎鑽 +10 經驗 +1 幫助 +1 技術 +1 收起 理由
allblue + 10 + 1 + 1 + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-2 18:53
由 手機網頁 發佈
以上程式是臨時弄的,所以很多{}中間沒有東西
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
 樓主| e79918 | 收聽TA | 只看該作者
發表於 2014-5-6 11:41
想請問一下若是以此張方式寫
public class SitesDBHlp extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "sites";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "sitesInfo";
        private static final String TABLE_CREATE =
                                        "CREATE TABLE " + TABLE_NAME + " ( " +
                                        " equipment TEXT NOT NULL, " +
                                        " staff TEXT NOT NULL, " +
                                        " date TEXT," +
                                        " time TEXT , PRIMARY KEY (equipment)); ";

我要加另一張資料表的話是在複寫一次這串嗎? 還是說是另一種寫法呢?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-6 13:07
e79918 發表於 2014-5-6 11:41
想請問一下若是以此張方式寫
public class SitesDBHlp extends SQLiteOpenHelper {
        private static final  ...

這樣寫我是沒試過
如果你這樣寫能成功Create sitesInfo 這個資料表
那你應該只要在加這兩列
private static final String TABLE_NAME2 = "sitesInfo2";
        private static final String TABLE_CREATE =
                                        "CREATE TABLE " + TABLE_NAME2 + " ( " +
                                        " equipment TEXT NOT NULL, " +
                                        " staff TEXT NOT NULL, " +
                                        " date TEXT," +
                                        " time TEXT , PRIMARY KEY (equipment)); ";
試試看吧
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
 樓主| e79918 | 收聽TA | 只看該作者
發表於 2014-5-10 09:46
新增進去  但是第二個資料表想存的東西還是不能存..我再找其他方法試試看囉

謝謝你^^
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

8
kittybear520 | 收聽TA | 只看該作者
發表於 2014-10-15 01:43
由 手機網頁 發佈
謝謝大大無私分享
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則