用java调用sqlite

网络111 刘修军

1、首先启动eclipse和android虚拟机,用adb shell命令行新建目录、SQLite数据库和表,并完成表记录的添加、删除、修改操作。

命令如下:

启动ADB

(1)编写一个批处理文件runadb.bat

path F:eclipse 3.7android-sdksplatform-tools

adb shell

(2) 新建文件夹

在/data/data/目录下,建立了目录“mySQLite.com/databases”

#cd data /data

#mkdir mySQLite.com

#cd mySQLite.com

#mkdir databases

#cd databases

(3)新建数据库

#sqlite3 test.db

新建数据库后,提示符变成“sqlite>”

(4)新建表

sqlite>create table contacter("no" INTEGER,"name" TEXT,"tel" TEXT);

(5) 查看表结构

sqlite>select * form sqlite_master where type="table" and name="表名";

(6)添加记录

sqlite> insert into contacter("no","name","tel") values(1001,"张三","15111114444");

(7)显示记录

sqlite>.explain ON

sqlite>select * from contacter;

2、编写android程序实现表记录的添加、查询。

 1 import android.content.Context;
 2 import android.database.sqlite.SQLiteDatabase;
 3 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 4 import android.database.sqlite.SQLiteOpenHelper;
 5 
 6 public class SQLHelper extends SQLiteOpenHelper {
 7 
 8     public SQLHelper(Context context, String name, CursorFactory factory,
 9             int version) {
10         super(context, name, factory, version);
11         // TODO Auto-generated constructor stub
12     }
13 
14     @Override
15     public void onCreate(SQLiteDatabase arg0) {
16         // TODO Auto-generated method stub
17 
18     }
19 
20     @Override
21     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
22         // TODO Auto-generated method stub
23 
24     }
25 
26 }
View Code
 1 import android.app.Activity;
 2 import android.database.Cursor;
 3 import android.database.sqlite.SQLiteDatabase;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.widget.TextView;
10 
11 public class SqlitActivity extends Activity implements OnClickListener {
12     /** Called when the activity is first created. */
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17         btnAdd=(Button) findViewById(R.id.btn);
18         btnDel=(Button) findViewById(R.id.btnDel);
19         btnUpdate=(Button) findViewById(R.id.btnUpdate);
20         cha=(Button) findViewById(R.id.chaxun);
21         btnAdd.setOnClickListener(this);
22         btnDel.setOnClickListener(this);
23         btnDel.setOnClickListener(this);
24         cha.setOnClickListener(this);
25           de=(EditText) findViewById(R.id.edt);
26         
27     }
28      Button btnAdd,btnDel,btnUpdate ,cha;
29      EditText de;
30     
31      
32     @Override
33     public void onClick(View arg0) {
34         // TODO Auto-generated method stub
35         Button b=(Button) arg0;
36          int  id=b.getId();
37          SQLHelper heple=new SQLHelper(this,"test.db",null,1);
38          SQLiteDatabase db=heple.getWritableDatabase();//
39          if(id==R.id.btn)
40          {
41              String sql="insert into lxr(no,name,tel) values('001','liming','1233444')";
42              db.execSQL(sql);     
43          }
44           if(id==R.id.chaxun)
45          {
46              Cursor cursor=db.query("lxr", new String[]{"*"},"name=?",new String[]{"liming"}, null, null, null);
47             // db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
48              String s="查询结果
";
49              String d="eeee";
50              while(cursor.moveToNext())
51              {
52                   String no= cursor.getString( cursor.getColumnIndex("no"));
53                  String name=cursor.getString(cursor.getColumnIndex("name"));
54                  String tel=cursor.getString(cursor.getColumnIndex("tel"));
55                  s+=no+","+name+","+tel+"
";
56             
57                  
58                  
59              }
60              de.setText(s); 
61         
62             
63          }
64         
65          
66         
67     }
68 }
View Code
原文地址:https://www.cnblogs.com/LXJ416/p/3150013.html