关系数据库Ormlite

来源:互联网 发布:网络基础知识教程视频 编辑:程序博客网 时间:2024/06/05 07:28
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();}@Overridepublic 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;}@Overridepublic void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {Log.d(this.getClass().getName(), "ORMLite数据库 -> onCreate");try {TableUtils.createTableIfNotExists(connectionSource, students.class);} catch (Exception e) {e.printStackTrace();}}@Overridepublic 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;}@Overridepublic 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