关系数据库Ormlite
2015-12-17 09:08
197 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/l__yx/article/details/50337207
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 >
相关文章推荐
- Android 使用ORMLite 操作数据库
- Android数据库ORMlite框架翻译系列(第二章:part 1)
- Android数据库使用(ORMLite)
- Android 数据库框架ormlite 使用精要
- Android进阶之数据库框架ormlite使用
- Android数据库框架-----ORMLite关联表的使用
- Android数据库ORMlite框架翻译系列(第一章)
- Android 使用ORMLite 操作数据库
- Android 数据库框架ormlite 使用精要
- ServiceStack OrmLite 数据库查询 几个实用方法 (继承表格式化集合等)
- 【光速使用开源框架系列】数据库框架OrmLite
- Android 使用ORMLite 操作数据库
- Android数据库ORMlite框架翻译系列(第二章:part 1)
- 安卓数据库ORMLite框架总结
- Android数据库ORMlite框架翻译系列(第一章)
- Android数据库ORMLite框架的注解详解
- ORMLite数据库框架的简单使用
- Android数据库ORMlite框架翻译系列(第二章:part 2)
- Android ORMLite 框架的入门用法-数据库
- Android 数据库框架ormlite 使用精要