1、DBAdapter类:?
?
1 package com.cnzcom.android.quickdial; 2 3 ?import android.content.ContentValues; 4 ?import android.content.Context; 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 @SuppressWarnings("unused") 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 "create table quickdial (key_id INTEGER PRIMARY KEY, " 64 + "position INTEGER, " 65 + "name TEXT, " 66 + "phone_number TEXT, " 67 + "ip INTEGER" 68 + ");"; 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 context = ctx; 87 DBHelper = new DatabaseHelper(context); 88 89 90 } 91 92 93 94 95 } 96 97 @Override 98 99 100 db.execSQL(DATABASE_CREATE); 101 } 102 103 @Override 104 105 106 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 107 onCreate(db); 108 } 109 110 } 111 112 113 114 115 116 117 118 { 119 db = DBHelper.getWritableDatabase(); 120 121 Cursor cursor = getAll(); 122 123 124 125 Log.e("数据库为空", "插入数据"); 126 127 128 129 130 insert(i, "", "", 0); 131 } 132 } else { 133 Log.e("数据库不为空", "读取数据"); 134 } 135 cursor.close(); 136 137 138 } 139 140 141 142 143 144 { 145 DBHelper.close(); 146 } 147 148 149 150 151 152 153 154 ContentValues initialValues = new ContentValues(); 155 initialValues.put(POSITION, position); 156 initialValues.put(NAME, name); 157 initialValues.put(PHONE_NUMBER, phone_number); 158 initialValues.put(IP, ip); 159 160 161 } 162 163 164 165 166 167 168 ContentValues initialValues = new ContentValues(); 169 initialValues.put(POSITION, position); 170 initialValues.put(NAME, ""); 171 initialValues.put(PHONE_NUMBER, ""); 172 initialValues.put(IP, 0); 173 174 175 176 177 } 178 179 180 181 182 183 184 ContentValues initialValues = new ContentValues(); 185 186 initialValues.put(NAME, name); 187 initialValues.put(PHONE_NUMBER, phone_number); 188 initialValues.put(IP, ip); 189 190 191 } 192 193 194 195 Cursor cur = db.query(DATABASE_TABLE, 196 197 198 199 } 200 201 202 Cursor cur = db.query(true, DATABASE_TABLE, new String[] {POSITION, 203 NAME, 204 PHONE_NUMBER, 205 IP}, 206 207 POSITION + "=" + rowId, null, null, null, null, null); 208 209 210 cur.moveToFirst(); 211 } 212 213 214 } 215 216 } |
?
2、Activity中调用:
?
?
1 public DBAdapter m_DBAdapter; 2 3 m_DBAdapter = new DBAdapter(this); 4 m_DBAdapter.open(); |