android sqlite数据库简测

 

简单测试sqlite数据的存储功能。

listview的item布局

listviewlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="name:" />


    <TextView
        android:id="@+id/list_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="age:" />


    <TextView
        android:id="@+id/list_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />


</LinearLayout>

MianActivity的布局

main.xml

   <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


        <Button
            android:id="@+id/store"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="store"
            android:text="存" />


        <Button
            android:id="@+id/insert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="insert"
            android:text="插入" />


        <Button
            android:id="@+id/select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="select"
            android:text="查询" />


        <Button
            android:id="@+id/delete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="delete"
            android:text="删除" />


        <Button
            android:id="@+id/replace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="replace"
            android:text="替换" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ll"
        android:orientation="vertical" >


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="列表" />


            <ListView


                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>


    </LinearLayout>

openhelper帮助类

public class MySqliteDataBase extends SQLiteOpenHelper {
    public final static  String dataName="datas";
    private String sql="create table student(_id Integer primary key autoincrement,name vachar(10),age Integer(10))";
public MySqliteDataBase(Context context,
CursorFactory factory) {
super(context, dataName, null, 1);
// TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
         db.execSQL(sql);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub


}


}

MainActivity:数据是写死的,未实现输入数据。

public class MainActivity extends AppCompatActivity implements OnClickListener,

OnItemClickListener {
private Button store, insert, select, delete, replace;
private MySqliteDataBase database;
private SQLiteDatabase sq;
private String[] names = { "a", "b", "c", "d" };
private int[] ages = { 1, 2, 3, 4 };
private ListView listview;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new MySqliteDataBase(this, null);
sq = database.getWritableDatabase();
listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(this);
store = (Button) findViewById(R.id.store);
store.setOnClickListener(this);
insert = (Button) findViewById(R.id.insert);
insert.setOnClickListener(this);
select = (Button) findViewById(R.id.select);
select.setOnClickListener(this);
delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(this);
replace = (Button) findViewById(R.id.replace);
replace.setOnClickListener(this);


}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.store:
for (int i = 0; i < names.length; i++) {
ContentValues values = new ContentValues();
values.put("name", names[i]);
values.put("age", ages[i]);
sq.insert("student", null, values);
}
select();
break;
case R.id.insert:
if (names.length < 20) {
ContentValues values = new ContentValues();
values.put("name", "e");
values.put("age", 5);
sq.insert("student", null, values);
}
select();
break;
case R.id.select:
select();
// c.close();
break;
case R.id.delete:
String[] whereArgs = { "e" };
// Cursor c1 = sq.query("student", null, null, null, null, null,
// null);
/**

Cursor cu = sq.query("student", null, null, null, null, null,
* null); StringBuilder sb = new StringBuilder(); if
* (cu.moveToFirst()) { // for (int i = 0; i < cu.getCount(); i++) {
* // cu.moveToPosition(i); while (cu.moveToNext()) {
* sb.append(cu.getString(1) + cu.getInt(2));
* System.out.println(cu.getColumnCount() + "----"); } }
* System.out.println(sb.toString()); Toast.makeText(this,
* sb.toString() + cu.getCount(), Toast.LENGTH_SHORT).show();
* cu.close();
*/
sq.delete("student", "name=?", new String[] { name });
select();
break;
case R.id.replace:
// ContentValues initialValues=new ContentValues();
// initialValues.put("age", 7);
// sq.replace("sudent", null, initialValues);
ContentValues values = new ContentValues();
values.put("name", "A");
String[] s = { "a" };
sq.update("student", values, "name=?", s);
select();
break;


default:
break;
}
}


private Cursor c;


private void select() {
// TODO Auto-generated method stub
String sql = "select * from student";
c = sq.rawQuery(sql, null);
String[] item = { "name", "age" };
int[] r = { R.id.list_name, R.id.list_age };
if (null != c) {
listview.setAdapter(new SimpleCursorAdapter(this,
R.layout.listviewlayout, c, item, r, 0));
}


}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
sq.close();
if(null!=c){
c.close();}
super.onDestroy();
}


private int pos;
private String name;


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
pos = position;
TextView t = (TextView) view.findViewById(R.id.list_name);
name = t.getText().toString();
Toast.makeText(this, pos + "被选中", Toast.LENGTH_SHORT).show();
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值