Xutils建立数据库,打印显示数据

1,一定要在mainfest.xml文件中添加权限,不然,就找不到建立的数据库

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

2,建立数据库表

package com.example.delet;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
@Table(name = "project")
public class ProjectDb {
@Column(name = "id", isId = true, autoGen = true)
private int _id;



// 项目名称
@Column(name = "project_name")
public String project_name;


// 测试地点
@Column(name = "address")
public String address;


// 测试人员
@Column(name = "name")
public String name;


// 测试天气
@Column(name = "weather")
public String weather;


public int get_id() {
return _id;
}


public void set_id(int _id) {
this._id = _id;
}



public String getProject_name() {
return project_name;
}


public void setProject_name(String project_name) {
this.project_name = project_name;
}




public String getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}




public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}





public String getWeather() {
return weather;
}


public void setWeather(String weather) {
this.weather = weather;
}

public String toString() {
return "ProjectDb [id=" + _id + ", name=" + name + ", weather=" + weather
+ ", adress=" + address + "]";
}


}

3,

在程序中创建一个数据库和如何生成表。

public class XUtil {
static DbManager.DaoConfig daoConfig;
public static DaoConfig getDaoConfig(){
File file=new File(Environment.getExternalStorageDirectory().getPath());
if(daoConfig==null){
daoConfig=new DbManager.DaoConfig()
.setDbName("shiyan.db")
.setDbDir(file)
.setDbVersion(1)
.setAllowTransaction(true)
.setDbUpgradeListener(new DbUpgradeListener() {
@Override
public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
}
});
}
return daoConfig;
}
}
4,在主main中进行调用这些数据库
package com.example.delet;


import org.xutils.DbManager;
import org.xutils.x;
import org.xutils.DbManager.DaoConfig;
import org.xutils.ex.DbException;






import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {
	
DaoConfig daoConfig=Xutils.getDaoConfig();
		DbManager db = x.getDb(daoConfig);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		insert();
		query();
	}
	
	private void insert() {
			try {
			ProjectDb show=new ProjectDb();
			
                   show.setName("测试开始");
show.setWeather("天晴");
show. setAddress(" 武汉");

db.show(person);

} catch (DbException e) {
e.printStackTrace();
}
}


private void query(){
try {
ProjectDb show = db.findById(ProjectDb.class, "1");

System.out.println(show.toString());
} catch (DbException e) {
e.printStackTrace();
}
}


}

最后运行结果是:在日志中打印出String()的内容,用第三方软件可以打开新建的表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值