安卓学习-数据存储与IO-Sqlite
直接用sql语句实现 ,添加、删除、修改
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{ Button btn1; Button btn2; Button btn3; EditText editText1; EditText editText2; SQLiteDatabase db; ListView listView1; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn3=(Button)findViewById(R.id.button3); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); editText1=(EditText)findViewById(R.id.editText1); editText2=(EditText)findViewById(R.id.editText2); listView1=(ListView)findViewById(R.id.listView1); try { File file= new File(getFilesDir().getCanonicalPath()+"/test.db"); if(file.exists()){ file.delete(); } //打开数据库,不存在则创建 db=SQLiteDatabase.openOrCreateDatabase(getFilesDir().getCanonicalPath()+"/test.db", null); String sql= "CREATE TABLE t_test ("+ " _id INTEGER NOT NULL,"+ " name TEXT,"+ " PRIMARY KEY (_id ASC)"+ " )"; db.execSQL(sql); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public void onClick(View paramView) { if(paramView==btn1){ int _id=Integer.parseInt(editText1.getText().toString()); String name=editText2.getText().toString(); String sql="select * from t_test where _id="+_id; Cursor cursor=db.rawQuery(sql, null); if(cursor.getCount()>0){ Toast.makeText(MainActivity.this, "记录已存在", Toast.LENGTH_SHORT).show(); }else{ sql="INSERT INTO t_test VALUES (?, ?)"; db.execSQL(sql, new Object[]{_id,name}); } }else if(paramView==btn2){ int _id=Integer.parseInt(editText1.getText().toString()); String name=editText2.getText().toString(); String sql="update t_test set _id=?,name=? where _id=?"; db.execSQL(sql, new Object[]{_id,name,_id}); }else if(paramView==btn3){ int _id=Integer.parseInt(editText1.getText().toString()); String sql="delete from t_test where _id=?"; db.execSQL(sql, new Object[]{_id}); editText1.setText(""); editText2.setText(""); } cursor=db.rawQuery("select * from t_test", null); SimpleCursorAdapter adapter=new SimpleCursorAdapter(MainActivity.this, R.layout.item, cursor, new String[]{"_id","name"}, new int[]{R.id.textView2,R.id.textView4}); listView1.setAdapter(adapter); listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) { if(cursor.moveToPosition(paramInt)){ editText1.setText(""+cursor.getInt(cursor.getColumnIndex("_id"))); editText2.setText(cursor.getString(cursor.getColumnIndex("name"))); } } }); } }
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="15dp" android:text="ID:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_marginLeft="33dp" android:layout_toRightOf="@+id/textView1" android:text="001" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_marginLeft="17dp" android:layout_toRightOf="@+id/textView2" android:text="名字:" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_marginLeft="27dp" android:layout_toRightOf="@+id/textView3" android:text="测试1" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_centerHorizontal="true" android:text="修改" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="21dp" android:text="删除" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_marginTop="11dp" android:text="添加" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/button1" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:ems="10" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/editText1" android:layout_marginTop="19dp" android:text="名字" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_alignParentTop="true" android:layout_marginTop="21dp" android:layout_toLeftOf="@+id/editText1" android:text="id" android:textAppearance="?android:attr/textAppearanceMedium" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/button2" > </ListView> </RelativeLayout>
Sqlite工具类
自动创建数据库,自动执行sql创建和升级语句,数据库版本号
MySQLiteOpenHelper.java
public class MySQLiteOpenHelper extends SQLiteOpenHelper { public static int dbVer=3; public static String dbname="test6.db"; public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO 自动生成的构造函数存根 } @Override public void onCreate(SQLiteDatabase db) { String sql= "CREATE TABLE t_test ("+ " _id INTEGER NOT NULL,"+ " name TEXT,"+ " PRIMARY KEY (_id ASC)"+ " )"; db.execSQL(sql); } @Override public void onConfigure(SQLiteDatabase db) { // TODO 自动生成的方法存根 super.onConfigure(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //升级,直到版本到最高 for (int j = oldVersion; j <= newVersion; j++) { if(j==2){ String sql = "alter table t_test add column column1 TEXT"; db.execSQL(sql); }else if(j==3){ String sql = "alter table t_test add column column2 TEXT"; db.execSQL(sql); } } } }
调用
MySQLiteOpenHelper dbHelper=new MySQLiteOpenHelper(this,MySQLiteOpenHelper.dbname,null,MySQLiteOpenHelper.dbVer); SQLiteDatabase db=dbHelper.getReadableDatabase();
其他都和上面的例子一样