`
yangguangfu
  • 浏览: 1530985 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android sqlite 一次创建多个表

阅读更多
package com.yangguangfu.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
 * 默认就在数据库里创建4张表
 * @author 阿福(trygf521@126.com)
 *
 */
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);
        
	}
	


}

 

数据库服务

package com.yangguangfu.database;


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;
/**
 * 数据库方法封装,创建表,删除表,数据(增删该查)...
 * @author 阿福(trygf521@126.com)
 *
 */
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 LinkedList<ConfigInfo> getScrollData(int startindex, int maxResult) {
//		LinkedList<ConfigInfo> xmlInfos = new LinkedList<ConfigInfo>();
//		Cursor cursor = dbOpenHelper.getReadableDatabase().rawQuery(
//				"select id,s,rt,st,ru,v,i from config limit ?,?,?,?,?,?",
//				new String[] { String.valueOf(startindex),
//						String.valueOf(maxResult) });
//		while (cursor.moveToNext()) {
//			ConfigInfo xmlInfo = new ConfigInfo();
//			xmlInfo.setId((cursor.getInt(0)));
//			xmlInfo.setS(cursor.getString(1));
//			xmlInfo.setRt(cursor.getString(2));
//			xmlInfo.setSt(cursor.getString(6));
//			xmlInfo.setRu(cursor.getString(3));
//			xmlInfo.setV(cursor.getString(4));
//			xmlInfo.setI(cursor.getString(5));
//			xmlInfos.add(xmlInfo);
//		}
//		cursor.close();
//		return xmlInfos;
//	}
//
//	// //////////////
//	public Cursor getScrollDataCursor(int startindex, int maxResult) {
//		Cursor cursor = dbOpenHelper
//				.getReadableDatabase()
//				.rawQuery(
//						"select id as _id,s,rt,li,ru,tn,m from config limit ?,?,?,?,?,?",
//						new String[] { String.valueOf(startindex),
//								String.valueOf(maxResult) });
//		return cursor;
//	}
//
//	public Cursor getScrollDataCursorApplication(int startindex, int maxResult) {
//		Cursor cursor = dbOpenHelper
//				.getReadableDatabase()
//				.rawQuery(
//						"select id as _id,s,tt,st,st,ru,v,i from application limit ?,?,?,?,?,?,?",
//						new String[] { String.valueOf(startindex),
//								String.valueOf(maxResult) });
//		return cursor;
//	}

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

}
 
5
1
分享到:
评论
4 楼 applezuoer 2013-08-02  
  
3 楼 xjjq2008 2013-02-11  
使用测试方法的吗?
2 楼 wilsonchen 2012-08-09  
zhangsheXIN_ 写道
这个为什么这样写,明明上面写了四句创建的语句,下面为什么还要写

对呀,我也想问!
1 楼 zhangsheXIN_ 2011-07-11  
这个为什么这样写,明明上面写了四句创建的语句,下面为什么还要写

相关推荐

    android SQLite 一个数据库创建多个表

    android SQLite 一个数据库创建多个表,并对指定的表进行增删改查操作,具体使用SQLite数据库时,可以将这个作为模版,在此基础上增加或减少数据库及表,操作起来非常的方便。

    Android SQLite 数据库详细介绍

    我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用软件时创建出应用使用到的数据库表结构及添加一些初始化...

    Android SQLite--小巧好用的SQLite GUI管理工具

    5、多个SQL编辑:现代标签是用来编辑和显示的查询语句和结果比较容易多个SQL查询。 SQL查询执行输入或加载到SQL他们编辑。然后按F9键运行该查询,或Ctrl + F9来运行当前行或选择只。 6、时间测量: SQL执行的时间会...

    Android Studio 2022+SQLite 5.4

    同时,Android Studio还支持多个Emulator和调试设备,并支持Google Play应用程序商店的发布和分发。Android Studio是一个功能强大而又易于使用的IDE,特别设计用于Android应用程序开发,使得开发人员能够快速开发和...

    AndroidSqlite数据库操作封装SQLiteUtils.zip

    一个简单的基于Android的Sqlite数据库的操作封装,它有如下的好处:便捷地创建表和增添表字段灵活的数据类型处理通过操作对象来insert或者update表记录支持多种查询方式,支持多表自定义的复杂查询,支持分页查询支持...

    SQLite4Unity包下载

    SQLite4Unity 是 Unity 中的一个 SQLite 数据库操作工具库,用于在 Unity 项目中访问 SQLite 数据库。它提供了许多可用于创建,查询和更新 SQLite 数据库的功能,而无需编写复杂的 SQL 查询语句。 SQLite4Unity 是...

    Android创建和使用数据库详细指南.doc

    数据库支持每个应用程序无论大小的生命线,除非你的应用程序只处理简单的数据,那么就需要一个数据库系统存储你的结构化数据,Android使用SQLite数据库,它是一个开源的、支持多操作系统的SQL数据库,在许多领域广泛...

    SQLite在同一个库中,动态建立多张不同的表

    SQLite在同一个库中,动态建立多张不同的表。需要打开android studio,1、依次选择菜单“File”——“New”——“New Project”,按提示新建一个项目(即Project); 2、项目创建完毕,再依次选择菜单“File”——...

    NimbusBase_Android_Tutorial:在用户的个人云上同步 SQLite 应用程序数据

    试试这个该应用程序基于 sqlite 构建,仅包含一个名为User 的表,您可以在Playground下探索该表。 要创建、检索、更新和删除新记录: 创建:点击右上角的插入按钮,填写一些字段,然后点击保存检索:名称和电子邮件...

    Storm:Android SQLite 数据库管理器

    通过@Annotations 轻松创建表格通过@Annotations 轻松迁移在插入、更新和选择操作方面接近原生的 SQLite 速度无需手动解析 Cursors / 无需手动初始化 ContentValues 直接的 API 支持多个数据库文件任何对象都可以是...

    新版Android开发教程.rar

    Android 是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。 Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 ...

    Android调用已有sqlite数据库登录帐号

    本代码实现在不创建数据库的情况下通过调用已有数据库实现连接数据库并登录系统。网上很多都是先创建数据库再连接,对于我这个项目完全无法使用,自己调了一天才连通。。。心酸。

    21天学习android开发教程之SQLite分页读取

    本文主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个分页栏(关于GridView的用法),用于把数据分页显示。 分页栏的pagebuttons.xml的源码...

    Android高级编程--源代码

    2.1.2 创建第一个Android活动 22 2.1.3 Android应用程序的类型 27 2.2 面向移动设备的开发 28 2.2.1 关于硬件设计的考虑事项 28 2.2.2 考虑用户环境 31 2.2.3 Android开发 32 2.3 To-Do List示例 35 2.4 ...

    Google Android SDK开发范例大全(完整版)

    Android 应用程序由一个或多个组件组成: 活动 具有可视 UI 的应用程序是用活动实现的。当用户从主屏幕或应用程序启动器选择一个应用程序时,就会开始一个动作。 服务 服务应该用于任何需要持续较长时间的应用程序...

    Android应用开发详解

    Android开发基础,讲述了Android开发环境的搭建、Android常用工具的使用和第一个Android应用程序的开发 第二篇 技术篇 第3章 Android中的资源访问 Android 中的资源访问,讲述了如何定义和访问Android中的外部...

    《Android高级编程》

    6.4 Android中的数据库 6.4.1 SQLite简介 6.4.2 Cursor和内容值 6.4.3 使用Android数据库 6.5 内容提供器简介 6.5.1 使用内容提供器 6.5.2 本地Android内容提供器 6.5.3 创建一个新的内容提供器 6.5.4 创建和使用...

    android开发揭秘PDF

    2.3 创建第一个Android项目——HeUoAndroid 2.3.1 创建HelloAndroid项目 2.3.2 运行HelloAndroid及模拟器的使用 2.3.3 调试HelloAndroid 2.4 小结 第二部分 基础篇 第3章 Android程序设计基础 3.1 Android程序框架 ...

    Android提高之SQLite分页读取实现方法

    一般来说,Android自身就...本文实例程序主要讲解了SQLite的基本用法,如:创建数据库,使用SQL命令查询数据表、插入数据,关闭数据库,以及使用GridView实现了一个分页栏(关于GridView的用法),用于把数据分页显示。

Global site tag (gtag.js) - Google Analytics