涉及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

Android的SQLite基本操作的更多相关文章

  1. Android 的 Sqlite基本操作

    在 SQL 数据库中保存数据 使用数据库 将数据保存到数据库对于重复或结构化数据(比如契约信息)而言是理想之选. 本课程假定您基本熟悉 SQL 数据库并且可帮助您开始在 Android 中使用 SQL ...

  2. android 一个SQLite数据库多个数据表的基本使用框架 (带demo)

    android 一个SQLite数据库多个数据表(带demo) 前言        demo演示        一.搭建        二.建立实体类        三.建立数据库操作类        ...

  3. Android之SQLite数据存储

    一.SQLite保存数据介绍 将数据库保存在数据库对于重复或者结构化数据(比如契约信息)而言是理想之选.SQL数据库的主要原则之一是架构:数据库如何组织正式声明.架构体现于用于创建数据库的SQL语句. ...

  4. android安卓Sqlite数据库实现用户登录注册

    看了很多别人写的安卓SQlite数据的操作代码,一点也不通俗易懂,我觉得我写的不错,而且安卓项目也用上了,所以在博客园里保存分享一下!建立一个类 并继承SQLiteOpenHelper public ...

  5. Android中SQLite数据库小计

    2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...

  6. android 对sqlite数据库的增删改查等各种操作

    转载:http://blog.csdn.net/vrix/article/details/6717090 package com.sqlite.main; import java.io.File; i ...

  7. Android学习---SQLite数据库的增删改查和事务(transaction)调用

    上一篇文章中介绍了手工拼写sql语句进行数据库的CRUD操作,本文将介绍调用sqlite内置的方法实现CRUD操作,其实质也是通过拼写sql语句. 首先,创建一个新的android项目: 其次,查看代 ...

  8. android数据库SQLite的设计模式

    Dao设计模式可能是使用最多的数据库的设计模式其基本思路是将数据库操作的代码 与设计代码分离以便于维护和升级.具体的实现方法是使用包,然后在设计代码中调 用数据库的操作代码,dao设计模式需要创建5个 ...

  9. Android使用SQLite数据库(2)

    打开SQLite数据库,首先要建立一个DatabaseHelper类的实例,然后,再获得数据库: DatabaseHelper mDBH; SQLiteDatabase db; mDBH = new ...

随机推荐

  1. Python 条件判断 和循环

    使用条件判断 if else # 条件派单 if else print('条件派单 if else') # s = input('请输入生日年号:') # birth = int(s) birth = ...

  2. 06: zabbix常见面试题

    1.1 zabbix架构 1.zabbix理论 1)Zabbix是一个企业级的.开源的.分布式的监控套件,Zabbix可以监控网络和服务的监控状况. 2)Zabbix利用灵活的告警机制,允许用户对事件 ...

  3. hashmap存储数据

    在HashMap中,为什么不能使用基本数据类型作为key? 其实和HashMap底层的存储原理有关,HashMap存储数据的特点是:无序.无索引.不能存储重复元素. 存储元素采用的是hash表存储数据 ...

  4. QThread::wait(),一直以来我以为它阻塞的是QThread对象,可是我现在明白,原来阻塞的是这个对象所在的线程(通常是主线程)——所有事情源于 QThread 的事件循环——如果使用继承QThread这一方法,QThread::quit()没有效果,因为这个线程根本就不需要事件循环

    近日,使用QThread,一些问题百思不得其解,看过大牛的文章,恍然大悟啊. 原文 http://hi.baidu.com/dbzhang800/item/c14c97dd15318d17e1f46f ...

  5. Alibaba开源组件-分布式流量控制框架sentinel初探

    Alibaba开源组件-分布式流量控制框架sentinel初探 2018年12月09日 18:23:11 SuperPurse 阅读数 1965更多 分类专栏: J2EE   版权声明:本文为博主原创 ...

  6. Python 并发网络库

    Python 并发网络库 Tornado VS Gevent VS Asyncio Tornado:并发网络库,同时也是一个 web 微框架 Gevent:绿色线程(greenlet)实现并发,猴子补 ...

  7. 111、什么是stack (Swarm18)

    参考https://www.cnblogs.com/CloudMan6/p/8119150.html   什么是 stack ?    在将这个之前先回顾一下前面部署WordPress的过程:     ...

  8. 转载: utm坐标和经纬度相互转换

    原文地址: https://blog.csdn.net/hanshuobest/article/details/77752279 //经纬度转utm坐标 int convert_lonlat_utm( ...

  9. 多线程之实现Runnable接口及其优点

    多线程之实现Runnable接口: 1.创建一个Runnable接口的实现类 2.在实现类中重写Runnable接口的run方法 3.创建一个Runnable接口实现类的对象 4.创建Thread类对 ...

  10. 自己实现一个简化版Mybatis框架

    MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码.本文完成的Mybatis功能比较简单,代码还有许多需要改进的地方,大家可以结合M ...