您的位置:首页 > 数据库

访问模拟器数据库

2015-10-27 15:14 225 查看
最后写activity里通过点击事件来操作这些方法

package com.example.sqlite1;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
private SQLiteOpenHelper hel;
private MyTab mytab;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hel = new Helper(this);
findViewById(R.id.b1).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mytab = new MyTab(hel.getWritableDatabase());
mytab.insert("ming", "1992-01-16");

}
});
findViewById(R.id.b2).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mytab = new MyTab(hel.getWritableDatabase());
mytab.update(101, "haha","2015-10-02");

}
});
findViewById(R.id.b3).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mytab = new MyTab(hel.getWritableDatabase());
mytab.delete(102);

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


我们要创建一个表首先要让一个类去继承SQLiteOpenHelper然后在oncreate方法里写建表语句

package com.example.sqlite1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Helper extends SQLiteOpenHelper {
private static final String DBNAME = "neusoft.db";
private static final int DBVERSION = 1;
private static final String TABLENAME = "mytab";

public Helper(Context context) {
super(context, DBNAME, null, DBVERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "create table "+TABLENAME+"(" +
"id integer primary key," +
"name varchar(50) not null," +
"birthday date not null" +
")";
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}


然后在写一个具体操作的类

package com.example.sqlite1;

import android.database.sqlite.SQLiteDatabase;

public class MyTab {
private String tableName = "mytab";
private SQLiteDatabase db;
public MyTab(SQLiteDatabase db){
this.db = db;
}

public void insert(String name, String birthday) {
String sql = "insert into " + tableName + "(name,birthday) values('"
+ name + "','" + birthday + "')";
db.execSQL(sql);
db.close();
}

public void update(int id, String name, String birthday) {
String sql = "update " + tableName + " set name='" + name + "',birthday='"
+ birthday + "' where id=" + id;
db.execSQL(sql);
db.close();
}

public void delete(int id) {
String sql = "delete from " + tableName + " where id=" + id;
db.execSQL(sql);
db.close();
}

}


package com.example.sqlite1;

import android.database.sqlite.SQLiteDatabase;

public class MyTab {
private String tableName = "mytab";
private SQLiteDatabase db;
public MyTab(SQLiteDatabase db){
this.db = db;
}

public void insert(String name, String birthday) {
//String sql = "insert into " + tableName + "(name,birthday) values('"
//        + name + "','" + birthday + "')";
String sql = "insert into " + tableName + "(name,birthday) values(?,?)";
String[] s = {name,birthday};
db.execSQL(sql,s);
db.close();
}

public void update(int id, String name, String birthday) {
//        String sql = "update " + tableName + " set name='" + name + "',birthday='"
//                + birthday + "' where id=" + id;
String sql = "update " + tableName + " set name=?,birthday=?,id=?";
Object[] o = {name,birthday,id};
db.execSQL(sql,o);
db.close();
}

public void delete(int id) {
String sql = "delete from " + tableName + " where id=" + id;
db.execSQL(sql);
db.close();
}

}


使用ContentValues加上自带的增删改方法

package com.example.sqlite2;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class MyTab {
private String tableName = "mytab";
private SQLiteDatabase db;
public MyTab(SQLiteDatabase db){
this.db = db;
}

public void insert(String name, String birthday) {
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("birthday", birthday);
db.insert(tableName, null, cv);
db.close();
}

public void update(int id, String name, String birthday) {
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("birthday", birthday);
String where = " id=?";
String [] s ={String.valueOf(id)};
db.update(tableName, cv, where, s);
db.close();
}

public void delete(int id) {
ContentValues cv = new ContentValues();
String where = " id=?";
String [] s ={String.valueOf(id)};
db.delete(tableName, where, s);
db.close();
}

}


首先要把D:\adt-bundle-windows-x86-20131030\sdk\platform-tools这个路径配到path环境变量里

然后进去cmd 键入adb shell就进入到模拟器了

然后访问data/data/包名/databases

再键入sqlite3 数据库名.db

就可以对数据进行操作了

.schema是查看表
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: