Android SQLite服务--创建、增删改查

<pre name="code" class="java">import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
 * 默认就在数据库里创建4张表
 *
 *
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    private static final String name = "database.db";//数据库名称
    private static final int version = 1;//数据库版本号

    public DBOpenHelper(Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e("DBOpenHelper", "DBOpenHelperDBOpenHelperDBOpenHelperDBOpenHelper");
        db.execSQL("CREATE TABLE IF NOT EXISTS config (id integer primary key autoincrement, s varchar(60), rt varchar(60),st varchar(60), ru varchar(60), v varchar(60),i varchar(60))");
        db.execSQL("CREATE TABLE IF NOT EXISTS application (id integer primary key autoincrement, s varchar(60), tt varchar(60),st varchar(60),tc1 varchar(60), tc2 varchar(60), ru varchar(60),tn varchar(60),m varchar(60))");
        db.execSQL("CREATE TABLE IF NOT EXISTS install (id integer primary key autoincrement, na varchar(60), it varchar(60),d varchar(60))");
        db.execSQL("CREATE TABLE IF NOT EXISTS smslist (id integer primary key autoincrement, t varchar(60), st varchar(60),n1 varchar(60),n2 varchar(60),n varchar(60),m varchar(60),a varchar(60))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e("DBOpenHelper", "onUpgradeonUpgradeonUpgradeonUpgrade");
        db.execSQL("DROP TABLE IF EXISTS config");
        db.execSQL("DROP TABLE IF EXISTS application");
        db.execSQL("DROP TABLE IF EXISTS install");
        db.execSQL("DROP TABLE IF EXISTS smslist");
        onCreate(db);

    }

}
import android.content.Context;
import android.database.Cursor;

import com.yangguangfu.bean.ApplicationInfo;
import com.yangguangfu.bean.ConfigInfo;
import com.yangguangfu.bean.InstallInfo;
import com.yangguangfu.bean.SMSInfo;
/**
 * 数据库方法封装。创建表,删除表,数据(增删该查)...
 *
 *
 */
public class DatabaseService {
    private DBOpenHelper dbOpenHelper;

    public DatabaseService(Context context) {
        dbOpenHelper = new DBOpenHelper(context);
    }

    public void dropTable(String taleName) {
        dbOpenHelper.getWritableDatabase().execSQL(
                "DROP TABLE IF EXISTS " + taleName);

    }

    public void closeDatabase(String DatabaseName) {
        dbOpenHelper.getWritableDatabase().close();

    }
    public void createConfigTable() {
        String sql = "CREATE TABLE IF NOT EXISTS config (id integer primary key autoincrement, s varchar(60), rt varchar(60),st varchar(60), ru varchar(60), v varchar(60),i varchar(60))";
        dbOpenHelper.getWritableDatabase().execSQL(sql);
    }

    public void createTableApplication() {
        String sql = "CREATE TABLE IF NOT EXISTS application (id integer primary key autoincrement, s varchar(60), tt varchar(60),st varchar(60),tc1 varchar(60), tc2 varchar(60), ru varchar(60),tn varchar(60),m varchar(60))";
        dbOpenHelper.getWritableDatabase().execSQL(sql);
    }

    public void createTableInstall() {
        String sql = "CREATE TABLE IF NOT EXISTS install (id integer primary key autoincrement, na varchar(60), it varchar(60),d varchar(60))";
        dbOpenHelper.getWritableDatabase().execSQL(sql);
    }

    public void createTableSmslist() {
        String sql = "CREATE TABLE IF NOT EXISTS smslist (id integer primary key autoincrement, t varchar(60), st varchar(60),n1 varchar(60),n2 varchar(60),n varchar(60),m varchar(60),a varchar(60))";
        dbOpenHelper.getWritableDatabase().execSQL(sql);
    }

    public void saveConfigInfo(ConfigInfo configInfo) {
        dbOpenHelper.getWritableDatabase().execSQL(
                "insert into config (s, rt, st, ru, v,i) values(?,?,?

,?,?,?

)",
                new Object[] { configInfo.getS(), configInfo.getRt(),
                        configInfo.getSt(), configInfo.getRu(),
                        configInfo.getV(), configInfo.getI() });
    }

    public void saveApplicationInfo(ApplicationInfo configInfo) {
        dbOpenHelper
                .getWritableDatabase()
                .execSQL(
                        "insert into application (s,tt,tc1,tc2,ru,tn,m) values(?,?,?

,?,?,?,?

)",
                        new Object[] { configInfo.getS(), configInfo.getTt(),
                                configInfo.getTc1(), configInfo.getTc2(),
                                configInfo.getRu(), configInfo.getTn(),
                                configInfo.getM() });
    }

    public void saveMsmInfo(SMSInfo configInfo) {
        dbOpenHelper
                .getWritableDatabase()
                .execSQL(
                        "insert into smslist (t,st,n1,n2,n,m,a) values(?,?,?,?,?,?

,?)",
                        new Object[] { configInfo.getT(), configInfo.getSt(),
                                configInfo.getN1(), configInfo.getN2(),
                                configInfo.getN(), configInfo.getM(),
                                configInfo.getA() });
    }

    public void saveInstallInfo(InstallInfo configInfo) {
        dbOpenHelper.getWritableDatabase().execSQL(
                "insert into install (na,it,d) values(?,?

,?)",
                new Object[] { configInfo.getNa(), configInfo.getIt(),
                        configInfo.getD() });
    }

    public void updateConfigInfo(ConfigInfo configInfo) {
        dbOpenHelper.getWritableDatabase().execSQL(
                "update config set s=?

, rt=?, st=?, ru=?, v=?

,i=? where id=?",
                new Object[] { configInfo.getS(), configInfo.getRt(),
                        configInfo.getSt(), configInfo.getRu(),
                        configInfo.getV(), configInfo.getI(),
                        configInfo.getId() });
    }

    public void updateApplicationInfo(ApplicationInfo configInfo) {
        dbOpenHelper
                .getWritableDatabase()
                .execSQL(
                        "update application set s=?, tt=?

, st=?

, tc1=?, tc2=?,ru=?

,tn=?,m=? where id=?

",
                        new Object[] { configInfo.getS(), configInfo.getTt(),
                                configInfo.getSt(), configInfo.getTc1(),
                                configInfo.getTc2(), configInfo.getRu(),
                                configInfo.getTn(),configInfo.getM(), configInfo.getId() });
    }

    public void updateInstallInfo(InstallInfo configInfo) {
        dbOpenHelper.getWritableDatabase().execSQL(
                "update install set na=?, it=?, d=? where id=?",
                new Object[] { configInfo.getNa(), configInfo.getIt(),
                        configInfo.getD(), configInfo.getId() });
    }

    public void updateSMSInfo(SMSInfo configInfo) {
        dbOpenHelper
                .getWritableDatabase()
                .execSQL(
                        "update smslist set t=?

, st=?, n1=?, n2=?, n=?, m=?, a=? where id=?

",
                        new Object[] { configInfo.getT(), configInfo.getSt(),
                                configInfo.getN1(), configInfo.getN2(),
                                configInfo.getN(), configInfo.getM(),
                                configInfo.getA(), configInfo.getId() });
    }

    public void deleteItemData(String tableName, Integer id) {
        dbOpenHelper.getWritableDatabase()
                .execSQL("delete from " + tableName + " where id=?",
                        new Object[] { id });
    }

    public InstallInfo findInstallInfo(Integer id) {
        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(
                "select id,na,it,d from install where id=?

",
                new String[] { String.valueOf(id) });
        if (cursor.moveToNext()) {
            InstallInfo configInfo = new InstallInfo();
            configInfo.setId((cursor.getInt(0)));
            configInfo.setNa(cursor.getString(1));
            configInfo.setIt(cursor.getString(2));
            configInfo.setD(cursor.getString(3));

            return configInfo;
        }
        return null;
    }

    public ConfigInfo findConfigInfo(Integer id) {
        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(
                "select id,s,rt,st,ru,v,i from config where id=?",
                new String[] { String.valueOf(id) });
        if (cursor.moveToNext()) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setId((cursor.getInt(0)));
            configInfo.setS(cursor.getString(1));
            configInfo.setRt(cursor.getString(2));
            configInfo.setSt(cursor.getString(3));
            configInfo.setRu(cursor.getString(4));
            configInfo.setV(cursor.getString(5));
            configInfo.setI(cursor.getString(6));

            return configInfo;
        }
        return null;
    }

    public SMSInfo findSMSInfo(Integer id) {
        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(
                "select id,t,st,n1,n2,n,m,a from smslist where id=?",
                new String[] { String.valueOf(id) });
        if (cursor.moveToNext()) {
            SMSInfo configInfo = new SMSInfo();
            configInfo.setId((cursor.getInt(0)));
            configInfo.setT(cursor.getString(1));
            configInfo.setSt(cursor.getString(2));
            configInfo.setN1(cursor.getString(3));
            configInfo.setN2(cursor.getString(4));
            configInfo.setN(cursor.getString(5));
            configInfo.setM(cursor.getString(6));
            configInfo.setA(cursor.getString(7));

            return configInfo;
        }
        return null;
    }

    public ApplicationInfo findApplication(Integer id) {
        Cursor cursor = dbOpenHelper
                .getWritableDatabase()
                .rawQuery(
                        "select id,s,tt,st,tc1,tc2,ru,tn,m from application where id=?

",
                        new String[] { String.valueOf(id) });
        if (cursor.moveToNext()) {
            ApplicationInfo applicationinfo = new ApplicationInfo();
            applicationinfo.setId((cursor.getInt(0)));
            applicationinfo.setS(cursor.getString(1));
            applicationinfo.setTt(cursor.getString(2));
            applicationinfo.setSt(cursor.getString(3));
            applicationinfo.setTc1(cursor.getString(4));
            applicationinfo.setTc2(cursor.getString(5));
            applicationinfo.setRu(cursor.getString(6));
            applicationinfo.setTn(cursor.getString(7));
            applicationinfo.setM(cursor.getString(8));

            return applicationinfo;
        }
        return null;
    }

    public long getDataCount(String tableName) {
        Cursor cursor = dbOpenHelper.getReadableDatabase().rawQuery(
                "select count(*) from " + tableName, null);
        cursor.moveToFirst();
        return cursor.getLong(0);
    }

    public void close() {
        dbOpenHelper.close();
    }

}
				
时间: 2024-10-18 15:24:56

Android SQLite服务--创建、增删改查的相关文章

[Android] SQLite数据库之增删改查基础操作

    在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Unix等主流操作系统,同一时候可以跟非常多程序语言如C#.PHP.Java等相结合.以下先回想SQL的基本语句,再讲述Android的基本操作. 一. adb shell回想SQL语句     首先,我感觉自己整个大学印象最深的几门课就包含<数据库>,所以想先回想SQL增删改查的基本语句.而在And

android中SQLite数据库的增删改查

1.数据库帮助类PersonSQLiteOpenHelper package com.wzw.sqllitedemo.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper

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

上一篇文章中介绍了手工拼写sql语句进行数据库的CRUD操作,本文将介绍调用sqlite内置的方法实现CRUD操作,其实质也是通过拼写sql语句. 首先,创建一个新的android项目: 其次,查看代码实现增删查改: 1.创建DB工具类 MyDBHelper.java(创建数据库的操作) package com.amos.android_db; import android.content.Context; import android.database.sqlite.SQLiteDatabas

Android下利用SQLite数据库实现增删改查

1: 首先介绍如何利用adb查看数据库 1: adb shell 2: cd /data/data/包名/databases 3:  sqlite3 数据库 4   接下来就可以进行数据库的sql语法的使用了 bean对象: public class Person { private int id; private String name; private String number; } 数据库的创建以及表的创建: package com.example.db; import android.

使用HttpClient对ASP.NET Web API服务实现增删改查

本篇体验使用HttpClient对ASP.NET Web API服务实现增删改查. 创建ASP.NET Web API项目 新建项目,选择"ASP.NET MVC 4 Web应用程序". 选择"Web API". 在Models文件夹下创建Product类. public class Product { public int Id { get; set; } public string Name { get; set; } public string Categor

【Android基础篇】SQLite数据库的增删改查基本操作

一.概述 SQLite是Android系统的核心数据存储服务之一,它是一个轻型的嵌入式数据库,占用非常少的资源却能提供很好很快的数据存取服务,许多大型的需要数据存储的Android项目都有用到SQLite(也可以用于桌面应用程序). 下面介绍一下SQLite的创建数据库.表的操作,以及基本的增删改查操作. 二.基本操作API简介 在Android中,SQLiteDatabase类提供了SQLite的底层API,但在使用SQLite数据库时,我们往往不会直接操作SQLiteDatabase这个类,

SQLite中的增删改查

虽然android提供了sql查询的封装方法,但是理解起来还是麻烦,所以我这里用sql语句来完成工作. 首先是建立一个类,继承SQLiteOpenHelper 这里面会建立一个数据库,并且初始化一个表.当然你可以建立多个数据库,等以后再自行建立表也可以 DatabaseHelper.java package com.kale.sql; import android.content.Context; import android.database.sqlite.SQLiteDatabase; im

Android下数据库操作——增删改查

Android下数据库第一种方式增删改查 1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象 2.使用SqliteDatebase对象调用execSql()做增删改,调用rawQuery方法做查询. 特点:增删改没有返回值,不能判断sql语句是否执行成功.sql语句手动写,容易写错 private MySqliteOpenHelper mySqliteOpenHelper;     public InfoDao(Context co

Sqlite创建增删改查

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SQLite; using System.Data.Common; using System.IO; namespace Sqlite