涉及SQLite的增删改查,结果用log显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.sqlconnecttest;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class DBHelper extends SQLiteOpenHelper{
 
    /*
     * 必须有的构造器
     */
    public DBHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }
 
    /*
     * 当第一次创建数据库时,就调用该方法
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(createDatabases, 创建数据库--->);
    }
 
    /*
     * 当更新数据库时,调用该方法
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(updateDatabase, 更新数据库--->);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.example.sqlconnecttest;
 
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity implements OnClickListener{
 
    private Button createDatabase;
    private Button createTable;
    private Button insert;
    private Button select;
    private Button update;
    private Button delete;
     
    private final String DATABASE_NAME = myDatabase;
    private SQLiteDatabase mySQLiteDatabase = null;
    private final String TABLE_NAME = user;
    private String SQL =
            CREATE TABLE [user] (+
            [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, +
            [username] VARCHAR NOT NULL, +
            [password] VARCHAR NOT NULL, +
            [phoneNumber] VARCHAR NOT NULL);
    private DBHelper db;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createView();
        setListener();
    }
 
    private void createView(){
        createDatabase = (Button) findViewById(R.id.createDatabase);
        createTable = (Button) findViewById(R.id.createTable);
        insert = (Button) findViewById(R.id.insert);
        select = (Button) findViewById(R.id.select);
        update = (Button) findViewById(R.id.update);
        delete = (Button) findViewById(R.id.delete);
    }
    //加监听器
    private void setListener(){
        createDatabase.setOnClickListener(this);
        createTable.setOnClickListener(this);
        insert.setOnClickListener(this);
        select.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
    }
        //各种监听方法
    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.createDatabase:
        {
            db = new DBHelper(MainActivity.this,DATABASE_NAME,null,1);
            mySQLiteDatabase = db.getWritableDatabase();
            Log.i(数据库对象, mySQLiteDatabase.toString());
            //System.out.print(数据库对象+mySQLiteDatabase);
            break;
        }
        case R.id.createTable:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            try {
                mySQLiteDatabase.execSQL(SQL);
                Log.i(创建表, SQL);
            } catch (SQLException e) {
                e.printStackTrace();
                Log.i(创建表--》, 创建表失败----------------》);
            }
            break;
        }
        case R.id.insert:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(username, 老爸);
            cv.put(password, 123456);
            cv.put(phoneNumber, 134756658888);
            long n = mySQLiteDatabase.insert(TABLE_NAME, null, cv);
            Log.i(插入数据, n + );
            mySQLiteDatabase.close();
            break;
        }
        case R.id.select:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getReadableDatabase();
            /**
             * 参数1:表名
             * 参数2:要显示的列
             * 参数3:where语句
             * 参数4:where语句的条件值
             * 参数5:分组方式
             * 参数6:having条件
             * 参数7:排序方式
             */
            Cursor cursor = mySQLiteDatabase.query(TABLE_NAME,
                    new String[]{id,username,password,phoneNumber},
                    null, null, null, null, null);
            while(cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex(id));
                String username = cursor.getString(cursor.getColumnIndex(username));
                String password = cursor.getString(cursor.getColumnIndex(password));
                String phoneNumber = cursor.getString(cursor.getColumnIndex(phoneNumber));
                Log.i(query-->, id: + id + userName: + username +
                      password + password + phoneNumber + phoneNumber);
            }
            mySQLiteDatabase.close();
            break;
        }
        case R.id.update:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            ContentValues cv1 = new ContentValues();
            cv1.put(username, admin);
            cv1.put(password, admin);
            cv1.put(phoneNumber, 1388888);
            String whereClause = id + =?;
            String[] whereArgs = {8};
            /*
             * 参数1:表名
             * 参数2:是一个ContextValue对象,就是更新的值
             * 参数3:where语句条件
             * 参数4:where条件的值
             */
            int index = mySQLiteDatabase.update(TABLE_NAME, cv1, whereClause, whereArgs);
            Log.i(update-->, index + );
            break;
        }
        case R.id.delete:
        {
            db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
            mySQLiteDatabase = db.getWritableDatabase();
            /***
             * 参数1:表名
             * 参数2:where语句字段
             * 参数3:where语句字段的值
             */
            String whereClause1 = id + =?;
            String[] whereArgs1 = {1};
            int num = mySQLiteDatabase.delete(TABLE_NAME, whereClause1, whereArgs1);
            Log.i(删除记录-->, num + );
            mySQLiteDatabase.close();
            break;
        }
        default:
            break;
        }
    }
}
1
2
3
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.sqlconnecttest.MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
    <textview android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="SQLite数据库"><button android:id="@+id/createDatabase" android:layout_below="@id/textView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="创建一个数据库"></button><button android:id="@+id/createTable" android:layout_below="@id/createDatabase" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="创建一张表"></button><button android:id="@+id/insert" android:layout_below="@id/createTable" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="插入数据"></button><button android:id="@+id/select" android:layout_below="@id/insert" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="查询数据"></button><button android:id="@+id/update" android:layout_below="@id/select" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="更新数据"></button><button android:id="@+id/delete" android:layout_below="@id/update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="删除数据"></button></textview></relativelayout>

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

最新文章

  1. pandas.DataFrame对行和列求和及添加新行和列
  2. Ftrl in tensorflow
  3. [C#常用代码]类库中读取解决方案web.Config字符串
  4. resin4 发布war包
  5. Centos6.X下安装php nginx mysql 环境
  6. make menuconfig 是一个目录。停止 错误解决
  7. 读完了简明Python教程(a bite of Python)
  8. [R]django的HTTPREQUEST对象
  9. DataSet 中的数据排序 及 DataRow装成DataTable
  10. 为什么选择C++
  11. vue通过自定义指令 v-py 将名字转拼音
  12. leetcode目录
  13. Vivado HLS 工具
  14. Android学习笔记二之初始Activity
  15. Visual Studio未能加载“XX”包的解决方案
  16. iptables(2)
  17. day15-ajax和jquery
  18. Centos 6.5 freeswitch 编译mod_shout
  19. Django haystack+solr搜索引擎部署的坑.
  20. thymeleaf 标签的使用

热门文章

  1. 纯CSS3绘制神奇宝贝伊布动画特效
  2. 为什么说Python采用的是基于值的内存管理模式?
  3. ProGuard 最全混淆规则说明
  4. 使用TableSnapshotInputFormat读取Hbase快照数据
  5. DataX操作指南
  6. MySQL性能优化(六):分区
  7. sql server 函数详解(4)日期和时间函数
  8. 客户端相关知识学习(四)之H5页面如何嵌套到APP中
  9. java复制文件范例代码
  10. Delphi PopupMenu组件