当前位置: 移动技术网 > 移动技术>移动开发>Android > 一起学Android之Sqlite

一起学Android之Sqlite

2019年04月06日  | 移动技术网移动技术  | 我要评论

概述

android对sqlite提供了完全友好的支持,在应用程序内部,都可以通过名称访问任何的数据库。建议通过sqliteopenhelpe的子类并通过重写oncreate() 方法进行创建数据表。本文主要讲解andriod开发中sqlite的简单应用(增删改查),仅供学习分享使用。

涉及知识点

  1. sqliteopenhelper 管理创建数据库和版本管理的帮助类(抽象类)。
  2. oncreate 创建数据表的方法
  3. execsql 执行一个非select的语句,没有返回信息。
  4. sqlitedatabase 管理sqlite数据的对象,显示提供一组方法来管理数据库(crud)。
  5. simplecursoradapter 一个简易的适配器,用来将cursor适配数据到控件上。
  6. contentvalues 存储一组数据值(key-value)。

示例效果图

如下图所示:

数据库帮助类

代码如下:

 1 package com.hex.demosqlite;
 2 
 3 import android.content.context;
 4 import android.database.sqlite.sqlitedatabase;
 5 import android.database.sqlite.sqliteopenhelper;
 6 
 7 /**
 8  * created by administrator on 2019/4/4.
 9  */
10 public class databasehelper extends sqliteopenhelper {
11 
12     private static final string db_name="persons.db";
13 
14     private static final int db_version=1;
15 
16     public databasehelper(context context) {
17         super(context, db_name, null, db_version);
18     }
19 
20     /**
21      * 创建数据库,指定数据库名称,版本号
22      * @param db
23      */
24     @override
25     public void oncreate(sqlitedatabase db) {
26         string sql="create table person(_id integer primary key autoincrement not null, name char(10),nickname char(10))";
27         db.execsql(sql);
28     }
29 
30     /**
31      * 数据库升级
32      * @param db
33      * @param oldversion
34      * @param newversion
35      */
36     @override
37     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
38         if(newversion>oldversion){
39             oncreate(db);
40         }
41     }
42 }

新增(insert)

代码如下:

 1  /**
 2      * 新增
 3      *
 4      * @param v
 5      */
 6     public void add(view v) {
 7         /** //一般不使用此种方法,因为execsql方法没有返回值
 8          string sql="insert into person(name,nickname)values('宋江','及时雨')";
 9          db.execsql(sql);
10          */
11         contentvalues values = new contentvalues();
12         values.put("name", "宋江");
13         values.put("nickname", "及时雨");
14         //返回值为插入行的行号
15         long result = db.insert("person", null, values);
16         if (result > 0) {
17             toast.maketext(this, "插入成功", toast.length_short).show();
18         } else {
19             toast.maketext(this, "插入失败", toast.length_short).show();
20         }
21     }

查询(query)

代码如下:

 1   /**
 2      * 查询
 3      *
 4      * @param v
 5      */
 6     public void query(view v) {
 7         /*
 8         string sql="select * from person where name=?";
 9         cursor cursor = db.rawquery(sql,new string[]{"宋江"});
10         */
11         //columns 如果为空,则表示所有列
12         cursor cursor = db.query("person", null, "name=?", new string[]{"宋江"}, null, null, null);
13         while (cursor.movetonext()) {
14             int nameindex = cursor.getcolumnindex("name");
15             int nickindex = cursor.getcolumnindex("nickname");
16             string namevalue = cursor.getstring(nameindex);
17             string nickvalue = cursor.getstring(nickindex);
18             string s = "name=" + namevalue + ",nickname=" + nickvalue;
19             toast.maketext(this, s, toast.length_short).show();
20         }
21         adaper.swapcursor(cursor);
22         adaper.notifydatasetchanged();
23     }

修改(update)

代码如下:

 1     /**
 2      * 更新
 3      *
 4      * @param v
 5      */
 6     public void update(view v) {
 7         // string sql="update person set nickname=? where name=?";
 8 
 9         contentvalues values = new contentvalues();
10         values.put("nickname", "及时雨2");
11         //更新语句受影响的行数
12         int result = db.update("person", values, "name=?", new string[]{"宋江"});
13         if (result > 0) {
14             toast.maketext(this, "更新成功", toast.length_short).show();
15         } else {
16             toast.maketext(this, "更新失败", toast.length_short).show();
17         }
18     }

删除(delete)

代码如下:

 1   /**
 2      * 删除
 3      *
 4      * @param v
 5      */
 6     public void delete(view v) {
 7         //string sql="delete person where name=?";
 8 
 9         //受删除语句影响的行数
10         int result = db.delete("person", "name=?", new string[]{"宋江"});
11         if (result > 0) {
12             toast.maketext(this, "删除成功", toast.length_short).show();
13         } else {
14             toast.maketext(this, "没有删除成功", toast.length_short).show();
15         }
16     }

备注

千里之行,始于足下!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网