SQLite小例子

来源:互联网 发布:苹果下载不了淘宝 编辑:程序博客网 时间:2024/06/05 17:37

这是我在网站看到过一个网友做的SQLite一个小程序,觉得写的不错,以供提示参考,所以把转载下来了。其实就是一个小收藏夹。
 
代码如下:

ActivityMain_1.java
 

 

view plaincopy to clipboardprint?
01.public class ActivityMain_1 extends Activity { 
02.    //声明EditText实例 
03.    private EditText et1,et2,et3; 
04.    //声明Button实例 
05.    private Button button; 
06.     
07.    @Override 
08.    protected void onCreate(Bundle savedInstanceState) { 
09.        // TODO Auto-generated method stub 
10.        super.onCreate(savedInstanceState); 
11.        setContentView(R.layout.main_1); 
12.         
13.        //根据ID获得实例对象 
14.        et1 = (EditText) findViewById(R.id.EditText01); 
15.        et2 = (EditText) findViewById(R.id.EditText02); 
16.        et3 = (EditText) findViewById(R.id.EditText03); 
17.        button = (Button) findViewById(R.id.button); 
18.         
19.        //事件 
20.        button.setOnClickListener(new View.OnClickListener() { 
21.             
22.            @Override 
23.            public void onClick(View v) { 
24.                String name=et1.getText().toString(); 
25.                String url =et2.getText().toString(); 
26.                String desc = et3.getText().toString(); 
27.             
28.                ContentValues values = new ContentValues(); 
29.                 
30.                values.put("name", name); 
31.                values.put("url", url); 
32.                values.put("desc", desc); 
33.                 
34.                //实例化数据库帮助类 
35.                DBHelper helper = new DBHelper(getApplicationContext()); 
36.                //打开数据库 
37.                helper.open();  
38.                 
39.                //插入数据 
40.                helper.insert(values); 
41.                //实例化Intent 
42.                Intent intent = new Intent(ActivityMain_1.this,QueryActivity.class); 
43.                startActivity(intent); 
44.                helper.close(); 
45.            } 
46.        }); 
47.         
48.    } 
49.     
50.     
51.} 
 
 
QueryActivity.java
 

 

view plaincopy to clipboardprint?
01.public class QueryActivity extends ListActivity { 
02.     
03.    private ListView listView; 
04.    private Cursor c; 
05.      
06.    @Override 
07.    protected void onCreate(Bundle savedInstanceState) { 
08.        // TODO Auto-generated method stub 
09.        super.onCreate(savedInstanceState); 
10.        //setContentView(R.layout.row); 
11.         
12.        //实例化数据库帮助类 
13.        final DBHelper help = new DBHelper(this); 
14.        help.open(); 
15.        c= help.query(); 
16.         query(); 
17.        //提示对话框 
18.        final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
19.        listView.setOnItemClickListener(new OnItemClickListener() { 
20.            @Override 
21.            public void onItemClick(AdapterView<?> parent, View view, 
22.                    int position, long id) { 
23.                final long temp = id; 
24.                builder.setMessage("真的要删除该条记录吗?").setPositiveButton("是", new DialogInterface.OnClickListener() { 
25.                     
26.                    @Override 
27.                    public void onClick(DialogInterface dialog, int which) { 
28.                        System.out.println("yes"); 
29.                        help.del((int)temp); 
30.                        //重新查询 
31.                        //query(); 
32.                        Cursor c = help.query(); 
33.                        //列表项数组 
34.                        String[] from ={"_id","name","url","desc"}; 
35.                        //列表项ID 
36.                        int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3}; 
37.                        //适配器 
38.                        SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row,c,from,to); 
39.                        ListView listView = getListView(); 
40.                        listView.setAdapter(adapter); 
41.                         
42.                         
43.                    } 
44.                }).setNegativeButton("否", new DialogInterface.OnClickListener() { 
45.                     
46.                    @Override 
47.                    public void onClick(DialogInterface dialog, int which) { 
48.                        // TODO Auto-generated method stub 
49.                         
50.                    } 
51.                }); 
52.                builder.create(); 
53.                builder.show(); 
54.            } 
55.        }); 
56.         
57.    } 
58.    @Override 
59.    protected void onStop() { 
60.        // TODO Auto-generated method stub 
61.        super.onStop(); 
62.         
63.    } 
64.    public void query(){ 
65.        //列表项数组 
66.        String[] from ={"_id","name","url","desc"}; 
67.        //列表项ID 
68.        int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3}; 
69.         
70.        //适配器 
71.        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row,c,from,to); 
72.        //adapter.notifyDataSetChanged(); 
73.        //列表视图 
74.         listView = getListView(); 
75.        //为列表视图添加适配器 
76.        listView.setAdapter(adapter); 
77.         
78.    } 
79.     
80.     
81.} 
 

DBHelper.java
 

 

view plaincopy to clipboardprint?
01.public class DBHelper extends SQLiteOpenHelper { 
02.     
03.    private static final String DB_NAME="coll.db"; 
04.    //表名 
05.    private static final String TBL_NAME="logtable"; 
06.    //创建SQL语句 
07.    private static final String CREATE_TBL = "create table logtable " + 
08.            "(_id integer primary key autoincrement,name text,url text,desc text)"; 
09.    //SQLiteDatabase实例 
10.    private SQLiteDatabase db; 
11.     
12.    public DBHelper(Context context) { 
13.        super(context, DB_NAME, null, 2); 
14.         
15.    } 
16.    @Override 
17.    public void onCreate(SQLiteDatabase db) { 
18.         
19.        db.execSQL(CREATE_TBL); 
20.    } 
21.     
22.    //打开数据库 
23.    public void open(){ 
24.        db = getWritableDatabase(); 
25.    } 
26.     
27.    @Override 
28.    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
29.        // TODO Auto-generated method stub 
30.    } 
31.     
32.    //插入方法 
33.    public void insert(ContentValues values){ 
34.         
35.        db.insert(TBL_NAME, null, values); 
36.    } 
37.     
38.    //查询方法 
39.    public Cursor query(){ 
40.        Cursor c = db.query(TBL_NAME, null, null, null, null, null, null); 
41.        return c; 
42.    } 
43.     
44.    //删除方法 
45.    public void del(int id){ 
46.        db.delete(TBL_NAME, "_id=?", new String[]{String.valueOf(id)}); 
47.    } 
48.     
49.    //关闭数据库 
50.    public void close(){ 
51.        if (db != null){ 
52.            db.close(); 
53.        } 
54.    } 
55.} 
 

main_1.xml
 

 

view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03.    android:orientation="vertical" 
04.    android:layout_width="fill_parent" 
05.    android:layout_height="fill_parent" 
06.    > 
07.     
08.    <TextView 
09.        android:id="@+id/TextView01" 
10.        android:layout_width="fill_parent" 
11.        android:layout_height="wrap_content" 
12.        android:text="网站名称" 
13.    /> 
14.    <EditText 
15.        android:id="@+id/EditText01" 
16.        android:layout_width="fill_parent" 
17.        android:layout_height="wrap_content" 
18.         
19.    /> 
20.    <TextView 
21.        android:id="@+id/TextView02" 
22.        android:layout_width="fill_parent" 
23.        android:layout_height="wrap_content" 
24.        android:text="URL" 
25.    /> 
26.    <EditText 
27.        android:id="@+id/EditText02" 
28.        android:layout_width="fill_parent" 
29.        android:layout_height="wrap_content" 
30.    /> 
31.    <TextView 
32.        android:id="@+id/TextView03" 
33.        android:layout_width="fill_parent" 
34.        android:layout_height="wrap_content" 
35.        android:text="网站描述" 
36.    /> 
37.    <EditText 
38.        android:id="@+id/EditText03" 
39.        android:layout_width="fill_parent" 
40.        android:layout_height="wrap_content" 
41.        android:height="100px" 
42.    /> 
43.     
44.    <Button 
45.        android:id="@+id/button" 
46.        android:layout_width="wrap_content" 
47.        android:layout_height="wrap_content" 
48.        android:text="添加" 
49.    /> 
50.</LinearLayout> 
 
 
自定义布局:row.xml
 

 

view plaincopy to clipboardprint?
01.<?xml version="1.0" encoding="UTF-8"?> 
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
03.    android:orientation="horizontal" 
04.    android:layout_width="fill_parent" 
05.    android:layout_height="fill_parent" 
06.    android:layout_gravity="center_vertical" 
07.    > 
08.     
09.    <TextView 
10.        android:id="@+id/text0" 
11.        android:layout_width="wrap_content" 
12.        android:layout_height="wrap_content" 
13.        android:paddingRight="10px" 
14.    /> 
15.    <TextView 
16.        android:id="@+id/text1" 
17.        android:layout_width="wrap_content" 
18.        android:layout_height="wrap_content" 
19.        android:paddingRight="10px" 
20.    /> 
21.    <TextView 
22.        android:id="@+id/text2" 
23.        android:layout_width="wrap_content" 
24.        android:layout_height="wrap_content" 
25.        android:paddingRight="10px" 
26.    /> 
27.    <TextView 
28.        android:id="@+id/text3" 
29.        android:layout_width="wrap_content" 
30.        android:layout_height="wrap_content" 
31.        android:paddingRight="10px" 
32.    /> 
33.     
34.</LinearLayout> 
 

 

结果如图所示:

原创粉丝点击