您的位置:首页 > 移动开发 > Android开发

【Android应用开发】-(22)Excel数据导入Sqlite

2012-09-13 10:08 337 查看
大家好,好久不写博客。今天分享下如何将Excel的数据导入到Sqlite,最近做一个拨号程序,需要做来电提醒的功能,来电提醒有几种实现方式,第一种方式是通过一些开发的接口,通过提交号码进行查询,在没联网的情况下,这就不可能了,所以断然放弃。最后通过在本地构建一个存取号码段的数据库来进行查询。全国的电话号码段,数据量高达25万条。这些数据存在一个Excel表格中,这么大的数据量,在SqlServer或者Oracle中都好办,可Sqlite中可如何是好呢?肯定也有导入功能,于是经过一番学习。原来很简单,好了。说了一堆废话,看操作吧。

一、准备导入的环境设置
首先确定系统中是否有sqlite3,查看system/xlib目录。
# ls /system/xbin/sqlite3
没有的话请下载(包含两个文件,sqlite3,libncurses.so),将sqlite3拷贝到/system/xbin目录,将libncurses.so拷贝到/system/lib目录
到此,首先创建一个数据库:
# Sqlite3 /mnt/sdcard/test.db



我们看到现在已经进入sqlite了。接下来,输入.help可以看到所有命令及说明,刚兴趣的可移步到本文后面的附录中

二、准备数据源
我这里原本是一个Excel文件,通过另存为的方式保存为cvs文件。这里要特别注意!!使用记事本的方式打开cvs文件,将其另存为UTF-8,因为sqlite默认是UTF-8编码的,如果编码不一致,就乱码了。



创建一个表:create table phones(ID INTEGER PRIMARY KEY,MobileNumber INTEGER(7), MobileArea VARCHAR(16),MobileType VARCHAR(15),AreaCode VARCHAR(4));设置分割字符串# .separator ","三、导入数据# .import /mnt/sdcard/all.csv phones上述导入数据的方式,android系统中必须要sqlite3命令。更简单的方式是使用可视化数据库工具sqlitebrowser导入,简单快捷。导入成功后进行相关操作,发现25万条数据检索毫无压力,基本上是几百毫秒就可以搞定,而且13M的数据库压缩后连只有2M多一点。....四、附录sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source
distribution for further information.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices ?TABLE? Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY? Load an extension library
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.timeout MS Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐