网络111 刘修军
1、首先启动eclipse和android虚拟机,用adb shell命令行新建目录、SQLite数据库和表,并完成表记录的添加、删除、修改操作。
命令如下:
启动ADB
(1)编写一个批处理文件runadb.bat
path F:\eclipse 3.7\android-sdks\platform-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 importandroid.content.Context;2 importandroid.database.sqlite.SQLiteDatabase;3 importandroid.database.sqlite.SQLiteDatabase.CursorFactory;4 importandroid.database.sqlite.SQLiteOpenHelper;5
6 public class SQLHelper extendsSQLiteOpenHelper {7
8 publicSQLHelper(Context context, String name, CursorFactory factory,9 intversion) {10 super(context, name, factory, version);11 //TODO Auto-generated constructor stub
12 }13
14 @Override15 public voidonCreate(SQLiteDatabase arg0) {16 //TODO Auto-generated method stub
17
18 }19
20 @Override21 public void onUpgrade(SQLiteDatabase arg0, int arg1, intarg2) {22 //TODO Auto-generated method stub
23
24 }25
26 }
View Code
1 importandroid.app.Activity;2 importandroid.database.Cursor;3 importandroid.database.sqlite.SQLiteDatabase;4 importandroid.os.Bundle;5 importandroid.view.View;6 importandroid.view.View.OnClickListener;7 importandroid.widget.Button;8 importandroid.widget.EditText;9 importandroid.widget.TextView;10
11 public class SqlitActivity extends Activity implementsOnClickListener {12 /**Called when the activity is first created.*/
13 @Override14 public voidonCreate(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 @Override33 public voidonClick(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="查询结果\n";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+"\n";56
57
58
59 }60 de.setText(s);61
62
63 }64
65
66
67 }68 }
View Code