博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android之Sqlite数据库
阅读量:6278 次
发布时间:2019-06-22

本文共 5830 字,大约阅读时间需要 19 分钟。

数据库访问完毕后,游标必须也记得关闭

import com.huangzhong.love_power_model.UserInfoDto;import java.util.ArrayList;import java.util.List;import android.app.AlertDialog;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.util.Log;public class UserInfoDao{        private static final int MODE_PRIVATE = 0x0;    private static final String DATABASE = "lovepower";    private static SQLiteDatabase db;        public static void insert(Context context,UserInfoDto model) {        //Id,UserName,Birthday,PhoneNumber,QQ,Address,CreateDate        Cursor cursor;        int count = 0;        try {              db = context.openOrCreateDatabase(DATABASE, MODE_PRIVATE, null);             //db.execSQL("drop table UserInfo ;");              String sqlCheckExsit = "select count(*) as c from Sqlite_master  where type ='table' and name ='UserInfo' ";                cursor = db.rawQuery(sqlCheckExsit, null);                             if(cursor.moveToNext()){                        count = cursor.getInt(0);                        if(count <= 0){                                db.execSQL("Create table UserInfo (Id text not null,UserName text,Birthday text,PhoneNumber text,QQ text,Address text,CreateDate text);");                                  Log.e("insertSql", "2222");                     }             }             String insertSql = "insert into UserInfo (Id,UserName,Birthday,PhoneNumber,QQ,Address,CreateDate) " +                        "values('" + model.getId() + "', '"                        + model.getUserName()+"','"                        + model.getBirthday()+"','"                        + model.getPhoneNumber()+"','"                        + model.getQQ()+"','"                        + model.getAddress()+"','"                        + model.getCreateDate() + "');";                db.execSQL(insertSql);                cursor.close();            Log.e("insertSql", "ok");        }        finally {                        db.close();        }    }            /**     * 查询     * @param context 上下文     * @param sql       SQl查询语句     * @param fields  字段名集合     * @return    查询结果集 List
类型 */ public static List
query(Context context, String sql) { Log.e("UserInfoDto", "取数据"); List
dataList = new ArrayList
(); Cursor cursor;//Id,UserName,Birthday,PhoneNumber,QQ,Address,CreateDate try { db = context.openOrCreateDatabase(DATABASE, MODE_PRIVATE, null); cursor = db.rawQuery(sql, null); if (cursor != null) { while (cursor.moveToNext()) { UserInfoDto model =new UserInfoDto(); model.setId(cursor.getString(cursor.getColumnIndex("Id"))); model.setUserName(cursor.getString(cursor.getColumnIndex("UserName"))); model.setBirthday(cursor.getString(cursor.getColumnIndex("Birthday"))); model.setPhoneNumber(cursor.getString(cursor.getColumnIndex("PhoneNumber"))); model.setQQ(cursor.getString(cursor.getColumnIndex("QQ"))); model.setAddress(cursor.getString(cursor.getColumnIndex("Address"))); model.setCreateDate(cursor.getString(cursor.getColumnIndex("CreateDate"))); dataList.add(model); } cursor.close();//游标必须关闭 } }catch(Exception e) { new AlertDialog.Builder(context).setIcon(android.R.drawable.ic_dialog_alert).setTitle("数据库连接错误:") .setMessage("数据访问异常。").show(); } finally { db.close(); } return dataList; } /** * 数据记录总条数 * @param context 上下文 * @param sql SQL查询语句 * @return 记录条数 */ public static int getCount(Context context, String sql) { int totalCounty = 0; try { db = context.openOrCreateDatabase(DATABASE, MODE_PRIVATE, null); Cursor cursor = db.rawQuery(sql, null); cursor.moveToFirst(); totalCounty = cursor.getInt(0); cursor.close();//游标必须关闭 } catch (Exception e) { new AlertDialog.Builder(context) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("数据库连接错误:").setMessage("数据访问异常。").show(); } finally { db.close(); } return totalCounty; }}
数据访问层
public class UserInfoDto {    //Id,UserName,Birthday,PhoneNumber,QQ,Address,CreateDate    private String Id;    private String UserName;    private String Birthday;    private String PhoneNumber;    private String QQ;    private String Address;    private String CreateDate;    public String getId() {        return Id;    }    public void setId(String id) {        Id = id;    }    public String getUserName() {        return UserName;    }    public void setUserName(String userName) {        UserName = userName;    }    public String getBirthday() {        return Birthday;    }    public void setBirthday(String birthday) {        Birthday = birthday;    }    public String getPhoneNumber() {        return PhoneNumber;    }    public void setPhoneNumber(String phoneNumber) {        PhoneNumber = phoneNumber;    }    public String getQQ() {        return QQ;    }    public void setQQ(String qQ) {        QQ = qQ;    }    public String getAddress() {        return Address;    }    public void setAddress(String address) {        Address = address;    }    public String getCreateDate() {        return CreateDate;    }    public void setCreateDate(String createDate) {        CreateDate = createDate;    }}
实体层

 

转载于:https://www.cnblogs.com/huangzhen22/p/4803516.html

你可能感兴趣的文章
Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
查看>>
(转)第三方支付参与者
查看>>
程序员修炼之道读后感2
查看>>
DWR实现服务器向客户端推送消息
查看>>
js中forEach的用法
查看>>
Docker之功能汇总
查看>>
!!a标签和button按钮只允许点击一次,防止重复提交
查看>>
Maven学习总结(15)——Maven 项目中pom.xml详解
查看>>
【转】消息队列软件产品大比拼
查看>>
烂泥:使用snmpwalk采集设备的OID信息
查看>>
Codeforces #430 Div2 D
查看>>
BZOJ3168: [Heoi2013]钙铁锌硒维生素
查看>>
iOS开发之 -- oc和swift下输出乘法口诀表
查看>>
L145
查看>>
基于c的简易计算器一
查看>>
javascript运算符——位运算符
查看>>
Hadoop完全分布式环境下,DataNode进程正常启动,但是网页上不显示DataNode节点
查看>>
Mysql快速学习三 -----触发器
查看>>
TeKtronix TDS210数字示波器使用简介
查看>>
birt
查看>>