openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) 打開path文件所表明的文件
html
openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) 打開或建立(若是不存在時)path文件所表明的文件
java
openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory) 打開或建立File(若是不存在時)文件所表明的文件
android
以上方法均返回一個SQLiteDatabase對象!sql
execSQL(String sql, Object[] bindArgs) 執行sqlide
bindArgs表明sql中的佔位符參數的值
this
insert(String table, String nullColumnHack, ContentValues values) 插入語句,spa
table表名code
values爲ContentValues對象!該對象存儲方式爲Key-value,Key表示表中的字段名稱,value表示該字段對應的值sqlite
nullColumnHack表示強行向表中插入null,前提是第三個參數(values)爲空或者不包含任何Key-value時生效
update(String table, ContentValues values, String whereClause, String[] whereArgs) 更新語句
table爲表名
values爲ContentValues對象!該對象存儲方式爲Key-value,Key表示表中的字段名稱
value表示該字段對應的值,第三個參數爲條件例如:_ID = ? ,第四個參數爲第三個參數的值!
delete(String table, String whereClause, String[] whereArgs) 刪除數據
table表名
whereClause爲條件例如:_ID = ?
whereArgs爲whereClause的值!
query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) 查詢語句
table爲表名
columns想要顯示的字段
selection條件
selectionArgs 中爲selection的值
groupBy 分組
having 分組條件
orderBy 排序
limit 分頁
1 public class MainActivity extends Activity implements OnClickListener{ 2 private Button button,btnUpdate,btnDelete,btnQuery; 3 private static String DATABASE_PATH; 4 private static String DATABASE_SQL; 5 private SQLiteDatabase database; 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 initView(); 11 setEvent(); 12 } 13 14 private void setEvent() { 15 button.setOnClickListener(this); 16 btnUpdate.setOnClickListener(this); 17 btnDelete.setOnClickListener(this); 18 btnQuery.setOnClickListener(this); 19 } 20 21 private void initView() { 22 DATABASE_PATH = this.getFilesDir()+"/myDataBase.db"; 23 DATABASE_SQL = "CREATE TABLE USER_INOF(_ID INTEGER PRIMARY KEY,_NAME,_AGE,_ADDRESS)"; 24 database = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH, null); 25 button = (Button) findViewById(R.id.id_databaseButton); 26 btnUpdate = (Button) findViewById(R.id.id_databaseButtonUpdate); 27 btnDelete =(Button) findViewById(R.id.id_databaseButtonDelete); 28 btnQuery= (Button) findViewById(R.id.id_databaseButtonQuery); 29 } 30 31 @Override 32 public void onClick(View v) { 33 switch (v.getId()) { 34 case R.id.id_databaseButton: 35 //database.execSQL(DATABASE_SQL); 36 ContentValues values = new ContentValues(); 37 values.put("_NAME", "悟空"); 38 values.put("_AGE", "666"); 39 values.put("_ADDRESS", "遼寧"); 40 long i = database.insert("USER_INOF", null, values); 41 if(i==-1){ 42 Toast.makeText(this, "添加失敗", 0).show(); 43 }else{ 44 Toast.makeText(this, "添加成功", 0).show(); 45 } 46 break; 47 case R.id.id_databaseButtonUpdate: 48 ContentValues value = new ContentValues(); 49 value.put("_NAME", "豬八戒"); 50 value.put("_AGE", "555"); 51 value.put("_ADDRESS", "遼寧"); 52 database.update("USER_INOF", value, "_ID=?",new String[]{"2"}); 53 break; 54 case R.id.id_databaseButtonDelete: 55 int o = database.delete("USER_INOF", "_ID > ?", new String[]{"1"}); 56 Toast.makeText(this,""+o+"條數據受影響", 0).show(); 57 break; 58 case R.id.id_databaseButtonQuery: 59 Cursor cursor =database.query("USER_INOF", null, null, null, null, null, null); 60 if(cursor.getCount()>0){ 61 while (cursor.moveToNext()) { 62 String id = cursor.getString(0); 63 String name =cursor.getString(1); 64 String age =cursor.getString(2); 65 String address =cursor.getString(3); 66 Toast.makeText(this, "查詢成功:"+id+name+age+address, 0).show(); 67 } 68 }else{ 69 Toast.makeText(this,""+cursor.getCount()+"", 0).show(); 70 } 71 break; 72 default: 73 break; 74 } 75 } 76 77 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 6 <Button 7 android:id="@+id/id_databaseButton" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="添加數據" /> 11 12 <Button 13 android:id="@+id/id_databaseButtonUpdate" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="修改數據" /> 17 18 <Button 19 android:id="@+id/id_databaseButtonDelete" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="刪除數據" /> 23 24 <Button 25 android:id="@+id/id_databaseButtonQuery" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="查詢數據" /> 29 30 </LinearLayout>