关系数据库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
- 关系数据库Ormlite
- Ormlite数据库
- OrmLite数据库
- Android数据库ORMlite框架
- android Ormlite数据库更新
- Android ORMLite数据库简介
- Android数据库使用(ORMLite)
- Android数据库ORMlite框架
- 使用ormlite封装数据库
- Android ORMLite数据库简介
- ORMLite操作数据库
- Android 数据库框架ormlite
- Android 数据库ORMLite用法
- OrmLite数据库切换
- OrmLite数据库升级
- 数据库之OrmLite
- 数据库开源ormlite
- Android数据库框架ORMLite
- 上位机界面:pyqt
- SQL Server 通过备份文件初始化复制
- NBMA 和 BMA 区别
- java技术体系
- GitBook, Git + Markdown 快速发布你的书籍
- 关系数据库Ormlite
- SQL Server 复制:事务发布
- Android Broadcast
- 【用行动说话】第一篇博客
- K60_enet(1)——PHY与MAC
- iOS 3D Touch开发 很良心的版本原创!
- Sqlite3开发
- Android 短信验证码自动填写
- Strongswan5.3.3与win7 l2tpoverIPsecVPN对接的配置与打印