关系数据库Ormlite

package com.example.ormlite;

import java.util.List;
import com.j256.ormlite.dao.Dao;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{
	EditText ed1=null;
	EditText ed2=null;
	EditText ed3=null;
	EditText ed4=null;
	Button btn1=null;
	Button btn2=null;
	Dao<students, Integer> mUserDao;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ed1=(EditText) findViewById(R.id.edittextname);
		ed2=(EditText) findViewById(R.id.edittextid);
		ed3=(EditText) findViewById(R.id.edittextage);
		ed4=(EditText) findViewById(R.id.edittextgender);
		btn1=(Button) findViewById(R.id.buttonadd);
		btn1.setOnClickListener(this);
		btn2=(Button) findViewById(R.id.buttonchaxun);
		btn2.setOnClickListener(this);
		
		ORMLiteDatabaseHelper mDatabaseHelper = ORMLiteDatabaseHelper.getInstance(MainActivity.this);
		mUserDao= mDatabaseHelper.getUserDao();
	
	}

		
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonadd:
			add();
			break;
		case R.id.buttonchaxun:
			query();
			break;
		default:
			break;
		}
		
	}
	public void add(){
		students user = new students();
		user.students_id =Integer.parseInt(ed2.getText().toString());
		user.age = Integer.parseInt(ed3.getText().toString());
		user.name =ed1.getText().toString();
		user.gender=ed4.getText().toString();
		try {
			mUserDao.createOrUpdate(user);
		} catch (Exception e) {
		}
	}
	public void query(){
		List<students> users = null;

		// 全局查询
		try {
			users = mUserDao.queryForAll();
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (students user : users) {
			Log.d("数据库数据",user.students_id+","+user.name+","+user.age+","+user.gender);
		}
	
	}
}
package com.example.ormlite;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class ORMLiteDatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static ORMLiteDatabaseHelper mDatabaseHelper = null;
	private Dao<students, Integer> mUserDao = null;

	private final static String DataBase_NAME = "ormlite.db";
	private final static int DataBase_VERSION = 1;

	public ORMLiteDatabaseHelper(Context context, String databaseName,CursorFactory factory, int databaseVersion) {
		super(context, DataBase_NAME, factory, DataBase_VERSION);
	}

	public static ORMLiteDatabaseHelper getInstance(Context context) {
		if (mDatabaseHelper == null) {
			mDatabaseHelper = new ORMLiteDatabaseHelper(context, DataBase_NAME,null, DataBase_VERSION);
		}

		return mDatabaseHelper;
	}

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {

		Log.d(this.getClass().getName(), "ORMLite数据库 -> onCreate");

		try {
			TableUtils.createTableIfNotExists(connectionSource, students.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, ConnectionSource arg1,
			int arg2, int arg3) {
		
	}

	
	public Dao<students, Integer> getUserDao() {
		if (mUserDao == null) {
			try {
				mUserDao = getDao(students.class);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return mUserDao;
	}

	@Override
	public void close() {
		super.close();
		mUserDao = null;
	}
}

package com.example.ormlite;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "users")
public class students {

	public students() {

	}

	@DatabaseField(id = true, columnName = "students_id")
	public int students_id;

	@DatabaseField(columnName = "name")
	public String name;

	@DatabaseField(columnName = "age")
	public int age;
	
	@DatabaseField(columnName = "gender")
	public String gender;
}

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ormlite.MainActivity" >

    <EditText 
        android:id="@+id/edittextname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/name"/>
    <EditText 
        android:id="@+id/edittextid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/id"/>
	<EditText 
        android:id="@+id/edittextage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/age"/>
	<EditText 
        android:id="@+id/edittextgender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/gender"/>
	<LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    android:weightSum="2">
	    <Button 
	        android:id="@+id/buttonadd"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:text="@string/add"/>
	    <Button 
	        android:id="@+id/buttonchaxun"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:text="@string/chaxun"/>
	</LinearLayout>
</LinearLayout >


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值