006_01SQLite_demo

 1 package com.example.sqlitedemo;
 2
 3 import android.app.Activity;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.os.Bundle;
 7 import android.util.Log;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 import android.view.View;
11
12 public class MainActivity extends Activity {
13     SQLiteDatabase db = null;
14     public static String TAG = "sqlitedemo";
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19
20         db = SQLiteDatabase.openOrCreateDatabase(getFilesDir()+"/userinfo.db", null);
21
22         createTable(db);
23     }
24     public void insert(View v){
25         insert(db);
26     }
27     public void delete(View v){
28         delete(db);
29     }
30     public void update(View v){
31         update(db);
32     }
33     public void query(View v){
34         query(db);
35     }
36
37     private void update(SQLiteDatabase db){
38         String update = "update user set password=? where id = ?";
39         Object[] args = {"99999999", 5};
40         db.execSQL(update, args);
41     }
42
43     private void delete(SQLiteDatabase db){
44         String delete = "delete from user where id =?";
45         Object[] args = {5};
46         db.execSQL(delete, args);
47     }
48
49     private void query(SQLiteDatabase db){
50         String query = "select * from user";
51         Cursor resultSet = db.rawQuery(query, null);
52
53         int row = resultSet.getCount();
54         Log.i(TAG, row+"");
55
56         while(resultSet.moveToNext()){
57             int id = resultSet.getInt(0);
58             String username = resultSet.getString(1);
59             String password = resultSet.getString(2);
60             Log.i(TAG, id + "," +username+ "," + password);
61
62         }
63
64     }
65     private void insert(SQLiteDatabase db){
66         String insert = "insert into user values(?,?,?)";
67         Object[] args = {5, "user5", "55555555"};
68         db.execSQL(insert, args);
69     }
70
71     private void createTable(SQLiteDatabase db){
72         String createtable = "create table user(id int, name varchar(20),password char(8))";
73         db.execSQL(createtable);
74     }
75
76
77 }

MainActivity.java

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.sqlitedemo.MainActivity" >
11
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
16
17     <Button
18         android:layout_width="fill_parent"
19         android:layout_height="wrap_content"
20         android:onClick="insert"
21         android:text="insert" />
22
23     <Button
24         android:layout_width="fill_parent"
25         android:layout_height="wrap_content"
26         android:onClick="delete"
27         android:text="delete" />
28
29     <Button
30         android:layout_width="fill_parent"
31         android:layout_height="wrap_content"
32         android:onClick="update"
33         android:text="update" />
34
35     <Button
36         android:layout_width="fill_parent"
37         android:layout_height="wrap_content"
38         android:onClick="query"
39         android:text="query" />
40
41 </LinearLayout>

activity_main.xml

增删改查界面:

时间: 2024-10-14 01:01:40

006_01SQLite_demo的相关文章