1. 哪位成功编译了使用了sqlite3的android代码,能分享下方法吗
1、你在sqlite3建好库后放在assets资源文件下或者自定义文件下,拷贝到你自定义的路径然后打开,代码如下:
(1)private void File() {
LogUtils.i("考呗");
//判断数据库是否拷贝到相应的目录下
if (new File(fileName).exists() == false) {//判断是否存在此文件
File dir = new File(DB_PATH);//不存在则创建文件
if (!dir.exists()) {
dir.mkdir();
}
//复制文件
try {
InputStream is = this.getBaseContext().getAssets().open(DB_NAME);//要拷贝的文件在assets下
// InputStream is=this.getResources().openRawResource(
// R.raw.question);//要拷贝的文件在raw文件夹下
OutputStream os = new FileOutputStream(fileName);
LogUtils.i("拷贝成功");
//用来复制文件
byte[] buffer = new byte[8192];
//保存已经复制的长度
int length;
//开始复制
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
//刷新
os.flush();
//关闭
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)打开方法,成功打开后即可操作表(真机运行需root权限,虚拟机正常)
private SQLiteDatabase db;
db = SQLiteDatabase.openDatabase("fileName/xxx.db", null, SQLiteDatabase.OPEN_READWRITE);
2. 如何查看android 手机上sqlite3数据库
Android是有自带的类库的:SQLiteOpenHelper,使用的时候继承这个类,然后写逻辑就可以,一般使用单例模式:
public synchronized static DBHelper getDBHelper(Context context) {
if (helper == null) {
helper = new DBHelper(context);
}
return helper;
}
private DBHelper(Context context) {
super(context, "自己的数据库名", null, 数据库版本);
}
使用的时候也很简单,下面是一个删除操作:
public synchronized void deleteSite(String packname) {
SQLiteDatabase db = getWritableDatabase();
try {
db.beginTransaction();
db.delete("site", "packname=?", new String[] { packname });
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (db != null) {
}
}
}
3. 安卓怎么读取sqlite数据库
安装SQLite Expert Professional 可以在网上下载,我下载的是试用版,没有找注册码,但是试用不影响使用,反正用几次查看到数据库操作没有错误就不用这个工具了。当然也可以使用Eclipse插件DDMS来查看,也可以使用Android工具包中的adb工具来查看。android项目中的sqlite数据库位于/data/data/项目包/databases中。先介绍使用DDMS导出sqlite数据库的操作和使用adb工具访问sqlite数据库。
首先打开android项目的调试模式,然后找到显示DDMS
切换到DDMS,显示File Explorer窗口,找到/data/data/ 如下图1 ,
然后找到程序包的文件夹,打开databases,就能看到sqlite数据库文件了。选择将其导出。如下图2.
这样就把sqlite数据库文件以文件的方式导出来了,然后使用sqlite界面管理工具如SQLite Expert Professional可以打开该数据库了。其他 sqlite界面管理工具如sqlite administrator、sqlite man或者firefox插件sqlite manager也可以打开该数据库。
使用adb工具访问sqlite数据库
Android Debug Bridge(ADB)是Android的一个通用调试工具,它可以更新设备或模拟器中的代码,可以管理预定端口,可以在设备上运行shell命令,我们 知道android是基于linux内核,它的内部文件结构也是采用linux文件组织方式,因此访问它的文件结构需要使用shell。这次我们就会用 shell来访问android应用中的sqlite数据库文件。
1、运行cmd,切换到android-sdk目录,运行adb.exe,加上参数shell,出现#号就代表进入了shell命令模式,注意adb要在Android模拟器运行时才能进入shell:如下图
进入数据库所在目录:
shell命令记住两个基本命令ls和cd,类似windows命令提示行中的dir和cd,代表列出当前目录下文件列表和进入到指定目录。了解这两个命令之后,就可以找到data/data/项目包名/databases 如下图1, 找到数据库文件如下图2。
使用sqlite管理工具来进行数据库操作
键入sqlite3 数据库名就进入了sqlite管理模式了。
如下图
设置sqlite环境变量:
在android的sdk中自带了sqlite3.exe,这是sqlite的官方管理工具,它是一个命令行工具。为了使用方便,将其路径注册到系 统环境变量path中,即将;%Android_Home%加在Path中,这样只样运行sqlite3,就能直接打开sqlite管理工具了。
如下图:
8
使用sqlite管理数据库:
sqlite命令行工具默认是以;结束语句的。所以如果只是一行语句,要在末尾加;,或者在下一行中键入;,这样sqlite命令才会被执行。
sqlite常用命令:
.tables--查看数据库的表列表
.exit--退出sqlite命令行
其他命令可随时.help查看帮助。sql命令可直接在此命令行上执行即可:
4. 如何打开android的sqlite3
配置环境变量即可打开sqlite3
工具/原料
eclipse
jdk
android sdk
方法/步骤
首先将sdk ools目录下的sqlite3拷贝一份到sdkplatform-tools
然后打开环境变量 然后新建一个android的变量名和sdk所在目录下的platform-tools文件夹目录打进去 然后打上;号
5. 谁能给我发一个安卓带有SQLite数据库连接的能跑起来的小程序啊万分感谢。
Sqlite3是android自带的一种轻型的数据库,使用的话也是很方便的。 下面是例子: package yeshu.sqlite3; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class sqlite3 extends Activity { private Button createdatabase; private Button updatedatabase; private Button insert; private Button update; private Button select; private Button delete; private EditText edittext01; private EditText edittext02; private String name; private int id; private String dbname; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createdatabase = (Button) findViewById(R.id.xxx); updatedatabase = (Button)findViewById(R.id.Button02); insert = (Button)findViewById(R.id.Button03); update = (Button)findViewById(R.id.Button04); select = (Button)findViewById(R.id.Button05); delete = (Button)findViewById(R.id.Button06); edittext01 = (EditText)findViewById(R.id.EditText01); edittext02 = (EditText)findViewById(R.id.EditText02); createdatabase.setOnClickListener(new OnClickListener_create()); updatedatabase.setOnClickListener(new OnClickListener_updatedatabase()); insert.setOnClickListener(new OnClickListener_insert()); update.setOnClickListener(new OnClickListener_update()); select.setOnClickListener(new OnClickListener_select()); delete.setOnClickListener(new OnClickListener_delete()); } class OnClickListener_delete implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); db.delete("user", "id=?", new String[]{id+""}); } } class OnClickListener_insert implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub name = edittext02.getText().toString(); id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); ContentValues value = new ContentValues(); value.put("id", id); value.put("name", name); db.insert("user", null, value); System.out.println("insert"); } } class OnClickListener_update implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub name = edittext02.getText().toString(); id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); ContentValues value = new ContentValues(); value.put("name", name); db.update("user", value, "id=?", new String[]{id+""}); } } class OnClickListener_updatedatabase implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu", 2); SQLiteDatabase db = temp.getReadableDatabase(); } } class OnClickListener_select implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getReadableDatabase(); Cursor cursor = db.query("user", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()) { System.out.println(cursor.getShort(cursor.getColumnIndex("id"))); System.out.println(cursor.getString(cursor.getColumnIndex("name"))); } } } class OnClickListener_create implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getReadableDatabase(); } } } ================================ package yeshu.sqlite3; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class Data extends SQLiteOpenHelper { private static final int VERSION = 1; public Data(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public Data(Context context, String name, int version) { this(context, name, null, version); } public Data(Context context , String name) { this(context, name, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("create a new database"); db.execSQL("create table user(id int, name VERCHAR(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub System.out.println("update a database"); } }
6. android 5.0.1 数据库sqlite3怎么导入
1、运行输入
adb
shell
(前提是模拟器正在运行)。
2、进入命令界面后
输入
ls
指令
会列出文件的目录。
3、cd
进入你想要的目录里。
4、一层一层进去后会发现
databases目录
你的数据文件就在这个目录下放着。
5、sqlite3
test
(test就是你创建的数据库的名称
注意:不要加.db
后缀)。
6、现在你就进入你创建的test数据库了使用
.tables
就可以查看所有的表了。
7. 如何使用SQLite,Android上SQLite的最佳实践
SQLite3是目前最新的SQLite版本。可以从网站上下载SQLite3的源代码(本书使用的版本是sqlite-3.6.12.tar.gz)。
解压缩后进入sqlite-3.6.12的根目录,首先命令“./configure”生成Makefile文件,接着运行命令“make”对源代码进行编译,最后运行命令“make install”安装SQLite3。安装完毕后,可以运行命令sqlite3查看SQLite3是否能正常运行,如下所示:
[root@localhost ~]# sqlite3
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
可以看到,SQLite3启动后会停留在提示符sqlite>处,等待用户输入SQL语句。
在使用SQLite3前需要先了解下SQLite3支持的数据类型。SQLite3支持的基本数据类型主要有以下几类:
NULL
NUMERIC
INTEGER
REAL
TEXT
SQLite3会自动把其他数据类型转换成以上5类基本数据类型,转换规则如下所示:
char、clob、test、varchar—> TEXT
integer—>INTEGER
real、double、float—> REAL
blob—>NULL
其余数据类型都转变成NUMERIC
下面通过一个实例来演示SQLite3的使用方法。
新建一个数据库
新建数据库test.db(使用.db后缀是为了标识数据库文件)。在test.db中新建一个表test_table,该表具有name,、sex、age三列。SQLite3的具体操作如下所示:
[root@localhost home]# sqlite3 test.db
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table test_table(name, sex, age);
如果数据库test.db已经存在,则命令“sqlite3 test.db”会在当前目录下打开test.db。如果数据库test.db不存在,则命令“sqlite3 test.db”会在当前目录下新建数据库test.db。为了提高效率,SQLite3并不会马上创建test.db,而是等到第一个表创建完成后才会在物理上创建数据库。
由于SQLite3能根据插入数据的实际类型动态改变列的类型,所以在create语句中并不要求给出列的类型。
创建索引
为了加快表的查询速度,往往在主键上添加索引。如下所示的是在name列上添加索引的过程。
sqlite> create index test_index on test_table(name);
操作数据
如下所示的是在test_table中进行数据的插入、更新、删除操作:
sqlite> insert into test_table values ('xiaoming', 'male', 20);
sqlite> insert into test_table values ('xiaohong', 'female', 18);
sqlite> select * from test_table;
xiaoming|male|20
xiaohong|female|18
sqlite> update test_table set age=19 where name = 'xiaohong';
sqlite> select * from test_table;
xiaoming|male|20
xiaohong|female|19
sqlite> delete from test_table where name = 'xiaoming';
sqlite> select * from test_table;
xiaohong|female|19
批量操作数据库
如下所示的是在test_table中连续插入两条记录:
sqlite> begin;
sqlite> insert into test_table values ('xiaoxue', 'female', 18);
sqlite> insert into test_table values ('xiaoliu', 'male', 20);
sqlite> commit;
sqlite> select * from test_table;
xiaohong|female|19
xiaoxue|male|18
xiaoliu|male|20
运行命令commit后,才会把插入的数据写入数据库中。
数据库的导入导出
如下所示的是把test.db导出到sql文件中:
[root@localhost home]# sqlite3 test.db ".mp" > test.sql;
test.sql文件的内容如下所示:
BEGIN TRANSACTION;
CREATE TABLE test_table(name, sex, age);
INSERT INTO "test_table" VALUES('xiaohong','female',19);
CREATE INDEX test_index on test_table(name);
COMMIT;
如下所示的是导入test.sql文件(导入前删除原有的test.db):
[root@localhost home]# sqlite3 test.db < test.sql;
通过对test.sql文件的导入导出,可以实现数据库文件的备份。
11.2.2 SQLite3的C接口
以上介绍的是SQLite3数据库的命令操作方式。在实际使用中,一般都是应用程序需要对数据库进行访问。为此,SQLite3提供了各种编程语言的使用接口(本书介绍C语言接口)。SQLite3具有几十个C接口,下面介绍一些常用的C接口。
sqlite_open
作用:打开SQLite3数据库
原型:int sqlite3_open(const char *dbname, sqlite3 **db)
参数:
dbname:数据库的名称;
db:数据库的句柄;
sqlite_colse
作用:关闭SQLite3数据库
原型:int sqlite_close(sqlite3 *db)
例如:
test.c:
#include <stdio.h>
#include <sqlite3.h>
static sqlite3 *db=NULL;
int main()
{
int rc;
rc= sqlite3_open("test.db", &db);
if(rc)
{
printf("can't open database!\n");
}
else
{
printf("open database success!\n");
}
sqlite3_close(db);
return 0;
}
运行命令“gcc –o test test.c –lsqlite3”进行编译,运行test的结果如下所示:
[root@localhost home]# open database success!
sqlite_exec
作用:执行SQL语句
原型:int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *, char **errmsg)
参数:
db:数据库;
sql:SQL语句;
callback:回滚;
errmsg:错误信息
例如:
test.c:
#include <stdio.h>
#include <sqlite3.h>
static sqlite3 *db=NULL;
static char *errmsg=NULL;
int main()
{
int rc;
rc = sqlite3_open("test.db", &db);
rc = sqlite3_exec(db,"insert into test_table values('bao', 'male', 24)", 0, 0, &errmsg);
if(rc)
{
printf("exec fail!\n");
}
else
{
printf("exec success!\n");
}
sqlite3_close(db);
return 0;
}
编译完成后,运行test的结果如下所示:
[root@localhost home]# ./test
exec success!
[root@localhost home]# sqlite3 test.db
SQLite version 3.6.11
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from test_table;
bao|male|24
sqlite3_get_table
作用:执行SQL查询
原型:int sqlite3_get_table(sqlite3 *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn, char **pzErrmsg)
参数:
db:数据库;
zSql:SQL语句;
pazResult:查询结果集;
pnRow:结果集的行数;
pnColumn:结果集的列数;
errmsg:错误信息;
sqlite3_free_table
作用:注销结果集
原型:void sqlite3_free_table(char **result)
参数:
result:结果集;
例如:
test.c:
#include <stdio.h>
#include <sqlite3.h>
static sqlite3 *db=NULL;
static char **Result=NULL;
static char *errmsg=NULL;
int main()
{
int rc, i, j;
int nrow;
int ncolumn;
rc= sqlite3_open("test.db", &db);
rc= sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn,
&errmsg);
if(rc)
{
printf("query fail!\n");
}
else
{
printf("query success!\n");
for(i = 1; i <= nrow; i++)
{
for(j = 0; j < ncolumn; j++)
{
printf("%s | ", Result[i * ncolumn + j]);
}
printf("\n");
}
}
sqlite3_free_table(Result);
sqlite3_close(db);
return 0;
}
编译完成后,运行test的结果如下所示:
[root@localhost home]# ./test
query success!
xiaohong | female | 19 |
xiaoxue | female | 18 |
xiaoliu | male | 20 |
bao | male | 24 |
sqlite3_prepare
作用:把SQL语句编译成字节码,由后面的执行函数去执行
原型:int sqlite3_prepare(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **stmt, const char **pTail)
参数:
db:数据库;
zSql:SQL语句;
nByte:SQL语句的最大字节数;
stmt:Statement句柄;
pTail:SQL语句无用部分的指针;
sqlite3_step
作用:步步执行SQL语句字节码
原型:int sqlite3_step (sqlite3_stmt *)
例如:
test.c:
#include <stdio.h>
#include <sqlite3.h>
static sqlite3 *db=NULL;
static sqlite3_stmt *stmt=NULL;
int main()
{
int rc, i, j;
int ncolumn;
rc= sqlite3_open("test.db", &db);
rc=sqlite3_prepare(db,"select * from test_table",-1,&stmt,0);
if(rc)
{
printf("query fail!\n");
}
else
{
printf("query success!\n");
rc=sqlite3_step(stmt);
ncolumn=sqlite3_column_count(stmt);
while(rc==SQLITE_ROW)
{
for(i=0; i<2; i++)
{
printf("%s | ", sqlite3_column_text(stmt,i));
}
printf("\n");
rc=sqlite3_step(stmt);
}
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
编译完成后,运行test的结果如下所示:
[root@localhost home]# ./test
query success!
xiaohong | female | 19 |
xiaoxue | female | 18 |
xiaoliu | male | 20 |
bao | male | 24 |
在程序中访问SQLite3数据库时,要注意C API的接口定义和数据类型是否正确,否则会得到错误的访问结果。
8. sqlite android
1。你可以通过在console里直接用命令select,这个需要你的工具里有sqlite(或者更高的版本sqlite3)
2。也可以写个小AP,通过代码再在你的控件里show出来
3。把数据库导出来直接在你的桌面系统上看
9. 如何将SQLite3 创建的数据库与android 程序连接
Sqlite3是android自带的一种轻型的数据库,使用的话也是很方便的。
下面是例子:
package yeshu.sqlite3;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class sqlite3 extends Activity {
private Button createdatabase;
private Button updatedatabase;
private Button insert;
private Button update;
private Button select;
private Button delete;
private EditText edittext01;
private EditText edittext02;
private String name;
private int id;
private String dbname;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createdatabase = (Button) findViewById(R.id.xxx);
updatedatabase = (Button)findViewById(R.id.Button02);
insert = (Button)findViewById(R.id.Button03);
update = (Button)findViewById(R.id.Button04);
select = (Button)findViewById(R.id.Button05);
delete = (Button)findViewById(R.id.Button06);
edittext01 = (EditText)findViewById(R.id.EditText01);
edittext02 = (EditText)findViewById(R.id.EditText02);
createdatabase.setOnClickListener(new OnClickListener_create());
updatedatabase.setOnClickListener(new OnClickListener_updatedatabase());
insert.setOnClickListener(new OnClickListener_insert());
update.setOnClickListener(new OnClickListener_update());
select.setOnClickListener(new OnClickListener_select());
delete.setOnClickListener(new OnClickListener_delete());
}
class OnClickListener_delete implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
id = Integer.parseInt(edittext01.getText().toString());
Data temp = new Data(sqlite3.this, "data_yeshu");
SQLiteDatabase db = temp.getWritableDatabase();
db.delete("user", "id=?", new String[]{id+""});
}
}
class OnClickListener_insert implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
name = edittext02.getText().toString();
id = Integer.parseInt(edittext01.getText().toString());
Data temp = new Data(sqlite3.this, "data_yeshu");
SQLiteDatabase db = temp.getWritableDatabase();
ContentValues value = new ContentValues();
value.put("id", id);
value.put("name", name);
db.insert("user", null, value);
System.out.println("insert");
}
}
class OnClickListener_update implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
name = edittext02.getText().toString();
id = Integer.parseInt(edittext01.getText().toString());
Data temp = new Data(sqlite3.this, "data_yeshu");
SQLiteDatabase db = temp.getWritableDatabase();
ContentValues value = new ContentValues();
value.put("name", name);
db.update("user", value, "id=?", new String[]{id+""});
}
}
class OnClickListener_updatedatabase implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
Data temp = new Data(sqlite3.this, "data_yeshu", 2);
SQLiteDatabase db = temp.getReadableDatabase();
}
}
class OnClickListener_select implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
Data temp = new Data(sqlite3.this, "data_yeshu");
SQLiteDatabase db = temp.getReadableDatabase();
Cursor cursor = db.query("user", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null);
while(cursor.moveToNext())
{
System.out.println(cursor.getShort(cursor.getColumnIndex("id")));
System.out.println(cursor.getString(cursor.getColumnIndex("name")));
}
}
}
class OnClickListener_create implements OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
Data temp = new Data(sqlite3.this, "data_yeshu");
SQLiteDatabase db = temp.getReadableDatabase();
}
}
}
================================
package yeshu.sqlite3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Data extends SQLiteOpenHelper {
private static final int VERSION = 1;
public Data(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public Data(Context context, String name, int version)
{
this(context, name, null, version);
}
public Data(Context context , String name)
{
this(context, name, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
System.out.println("create a new database");
db.execSQL("create table user(id int, name VERCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("update a database");
}
}