网络112  陈永森

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

 

1 编写runadb.bat

 

  path D:\eclipse 3.7\android-sdks\platform-tools

 

  adb shell 

 

2 新建文件夹

 

  在data/data目录下创建cys.k(项目中包的名字),

 

  mkdir cys.k

 

  cd cys.k    在此目录下创建文件夹databases

 

  mkdir databases

 

  cd databases

 

3 创建数据库

 

  sqlite3 test.db

 

  create table 表名(“no” integer,”name” text,"tel" text);    //创建表

 

  select * from sqlite_master where type=”table” and name=”表名”;   //查询表结构

 

  insert into 表名 values(1,”张三”,"123456");

 

  .explain ON 

 

  Select * from 表名;    //查询

 

界面如下:

 

代码:

package cqvie.edu.cn;

import android.app.Activity;

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 SQLTestActivity extends Activity implements OnClickListener

{     /** Called when the activity is first created. */  

   @Override    

public void onCreate(Bundle savedInstanceState)

{

 super.onCreate(savedInstanceState);      

   setContentView(R.layout.main);    

     Button btnAdd=(Button) findViewById(R.id.btnAdd);   

     Button btnDel=(Button) findViewById(R.id.btnDel);  

     Button btnUpdate=(Button) findViewById(R.id.btnUpdate);

     Button btnQuery=(Button) findViewById(R.id.btnQuery);    

    btnAdd.setOnClickListener(this);     

    btnDel.setOnClickListener(this);     

    btnUpdate.setOnClickListener(this);      

   btnQuery.setOnClickListener(this);

    }

 @Override

 public void onClick(View arg0)

{ DBHelper helper=new DBHelper(this, "test.db", null, 1);

  SQLiteDatabase db=helper.getWritableDatabase();  

 Button btn=(Button)arg0;

  int id=btn.getId();

  if(id==R.id.btnAdd)  

 {    String sql="INSERT INTO contacter(no,name,tel)"+   "VALUES(2,'wang','65301234')";  

  db.execSQL(sql);}  

 else if(id==R.id.btnDel)

  {String sql="DELETE  FROM  contacter WHERE no=2";    

   db.execSQL(sql);  }  

 else if(id==R.id.btnUpdate)   

 {    String sql="UPDATE contacter SET name='ddd' where no=1";   

 db.execSQL(sql);   }  

 else if(id==R.id.btnQuery){

   Cursor cursor=db.query("contacter", new String[]{"*"}, "name=?", new String[]{"可儿"}, null, null, null);

   String s="查询结果\n";   

 while(cursor.moveToNext())  

  {     int no=cursor.getInt(cursor.getColumnIndex("no"));    

 String name=cursor.getString(cursor.getColumnIndex("name"));

    String tel=cursor.getString(cursor.getColumnIndex("tel"));   

  s+=no+","+name+","+tel+"\n";    }  

  EditText txtContent=(EditText) findViewById(R.id.txtContent);  

  txtContent.setText(s);   }

   db.close();  }     }

 

注意在测试之前一定要把数据库文件ttest。db的写权限打开,否则会出错。

在“/data/data/cys.k/databases”路径下执行“chmod 777 test.db”命令,修改test.db的权限