您的位置:首页 > 移动开发 > Android开发

ActiveAndroid数据库框架简单应用和配置

2015-08-28 10:57 721 查看
导入activeandroid-3.1-beta.jar包

实体类

@Table(name="Category")

public class Category extends Model{

@Column(name="Name")

public String name;

@Column(name="StuId")

public integer stuId;

}

@Table(name = "item")

public class Item extends Model {

@Column(name = "Name")

public String name;

@Column(name = "Category")

public Category category;

public Item() {

super();

}

public Item(String name, Category category) {

super();

this.name = name;

this.category = category;

}

}

Application文件

/**

* @描述:初始化数据库Application

* @author Zxy

* @date 2015年8月28日 上午10:15:39

*/

public class MyApplication extends Application{

@Override

public void onCreate() {

ActiveAndroid.initialize(this);

super.onCreate();

}

}

package com.my.ormdatabase.activity;

import com.activeandroid.query.Delete;

import com.activeandroid.query.Select;

import com.my.ormdatabase.pojo.Category;

import com.my.ormdatabase.pojo.Item;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//保存数据,保存数据(也是用save,修改之前他会先去检索表里面是否存在这条数据,存在的话,,会执行修改这条数据.)

Category cate = new Category();

cate.name = "***";

cate.save();

Item item = new Item();

item.name = "123123";

item.category = cate;

item.save();

//删除数据

// Item item2 = Item.load(Item.class, 1);

// item2.delete();

new Delete().from(Item.class).where("Name=?","123123").execute();

//对数据的查询方法

Log.i("msg", new Select().from(Item.class).where("Name=?","123123").orderBy("Name ASC").execute().size()+"");

/**

* 数据库升级

* 在assets目录下新建 migrations文件夹,修改AndroidManifest.xml里面版本号码

* 。,在migrations新建文件,文件名和版本号名一致 ALTER TABLE Category ADD COLUMN StuId INTEGER;

*/

}

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