ormlite的资料真少,网上很难找,就干脆自己看些api,以前以为这些api什马的很难,看下来之后觉得也不怎么困难。找了些操作数据库的类的用法,所以做了一个登录注册的小例子。
?
?
首先是登录相关的类
?
?
package com.cng;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.cng.dao.UsersDao;import com.cng.model.UsersEntity;import com.cng.utils.DataHelper;import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;import com.j256.ormlite.dao.Dao;import android.R.integer;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class OrmliteLoginDemoActivity extends OrmLiteBaseActivity<DataHelper> { private EditText username; private EditText password; private Button submit; private Button register; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); handlerbtn(); } public void initView() { username=(EditText)findViewById(R.id.username); password=(EditText)findViewById(R.id.password); submit=(Button)findViewById(R.id.submit); register=(Button)findViewById(R.id.register); } public void handlerbtn() { submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String usernameString=username.getText().toString(); String passwordString=password.getText().toString(); UsersDao usersDao=new UsersDao(); List<UsersEntity> userlist=new ArrayList<UsersEntity>(); try { Dao<UsersEntity, Integer> userDao=getHelper().getUserDataDao(); userlist=usersDao.findUser(userDao,usernameString, passwordString); } catch (SQLException e) { e.printStackTrace(); } if(userlist==null||userlist.size()<=0) { alert("用户名或密码错误"); } else { alert("欢迎登录"); Intent intent =new Intent(); intent.setClass(OrmliteLoginDemoActivity.this, welcom.class); startActivity(intent); } } }); register.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent =new Intent(); intent.setClass(OrmliteLoginDemoActivity.this, register.class); startActivity(intent); } }); } public void alert(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }
?
?然后是注册的
?
?
package com.cng;import java.sql.SQLException;import java.util.List;import com.cng.dao.UsersDao;import com.cng.model.UsersEntity;import com.cng.utils.DataHelper;import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;import com.j256.ormlite.dao.Dao;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class register extends OrmLiteBaseActivity<DataHelper>{ private EditText register_username; private EditText register_password; private EditText register_repassword; private Button register_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register); initview(); handlerbtn(); } public void initview() { register_username=(EditText)findViewById(R.id.register_username); register_password=(EditText)findViewById(R.id.register_password); register_repassword=(EditText)findViewById(R.id.register_repassword); register_btn=(Button)findViewById(R.id.register_btn); } public void handlerbtn() { register_btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String usernameString=register_username.getText().toString(); String passwordString=register_password.getText().toString(); String repasswordString=register_repassword.getText().toString(); if(usernameString.equals("")) { register_username.setError("请输入用户名"); return; } if(passwordString.equals("")) { register_password.setError("请输入密码"); return; } if(repasswordString.equals("")) { register_repassword.setError("请重复密码"); return; } if(!repasswordString.equals(passwordString)) { register_repassword.setError("重复密码与原密码不一致"); return; } UsersDao userDao=new UsersDao(); try { Dao<UsersEntity, Integer> userentitydao=getHelper().getUserDataDao(); List<UsersEntity> userlist=userDao.findUser(userentitydao, usernameString); if(userlist==null||userlist.size()<=0) { userDao.addsuer(userentitydao, usernameString, passwordString); alert("注册成功"); } else { alert("此账户已经被注册"); register_username.setText(""); } } catch (SQLException e) { e.printStackTrace(); } } }); } public void alert(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); }}
?
?
?
最后欢迎界面
?
?
package com.cng;import android.app.Activity;import android.os.Bundle;public class welcom extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcom); } }
?
?
再是数据库映射的实体类
?
?
package com.cng.model;import java.io.Serializable;import com.j256.ormlite.field.DatabaseField;import android.R.integer;public class UsersEntity implements Serializable{ private static final long serialVersionUID = -6582623980712135028L; @DatabaseField(generatedId=true) private int id; @DatabaseField private String username; @DatabaseField private String password; public UsersEntity(){} public UsersEntity(int id, String username, String password) { super(); this.id = id; this.username = username; this.password = password; } public int getId() { return id; } public String getPassword() { return password; } public String getUsername() { return username; } public void setId(int id) { this.id = id; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; }}?
?
再是操作数据库的类,我按照mvc的模式把操作数据库的类单独分离开了
?
?
package com.cng.dao;import java.sql.SQLException;import java.util.HashMap;import java.util.List;import java.util.Map;import com.cng.model.UsersEntity;import com.j256.ormlite.dao.Dao;public class UsersDao { public List<UsersEntity> findUser(Dao<UsersEntity, Integer> userdao,String username,String password) throws SQLException { Dao<UsersEntity, Integer> userDao = userdao; Map<String, Object> userMap=new HashMap<String, Object>(); userMap.put("username", username); userMap.put("password", password); List<UsersEntity> userlistEntities=userDao.queryForFieldValues(userMap); return userlistEntities==null?null:userlistEntities; } public List<UsersEntity> findUser(Dao<UsersEntity, Integer> userdao,String username) throws SQLException { Dao<UsersEntity, Integer> userDao = userdao; Map<String, Object> userMap=new HashMap<String, Object>(); userMap.put("username", username); List<UsersEntity> userlistEntities=userDao.queryForFieldValues(userMap); return userlistEntities==null?null:userlistEntities; } public void addsuer(Dao<UsersEntity, Integer> userdao,String username,String password) throws SQLException { Dao<UsersEntity, Integer> userDao = userdao; UsersEntity usersEntity=new UsersEntity(); usersEntity.setUsername(username); usersEntity.setPassword(password); userDao.create(usersEntity); }}?
最后是数据库管理类
?
?
?
package com.cng.utils;import java.sql.SQLException;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.util.Log;import com.cng.model.UsersEntity;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 DataHelper extends OrmLiteSqliteOpenHelper{ private static final String DATABASE_NAME = "OrmliteLoginDemo.db"; private static final int DATABASE_VERSION = 1; private Dao<UsersEntity, Integer> userDao = null; public DataHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, UsersEntity.class); } catch (SQLException e) { Log.e(DataHelper.class.getName(), "创建数据库失败", e); e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int arg2, int arg3) { try { TableUtils.dropTable(connectionSource, UsersEntity.class, true); onCreate(db, connectionSource); } catch (SQLException e) { Log.e(DataHelper.class.getName(), "更新数据库失败", e); e.printStackTrace(); } } @Override public void close() { super.close(); userDao = null; } public Dao<UsersEntity, Integer> getUserDataDao() throws SQLException { if (userDao == null) { userDao = getDao(UsersEntity.class); } return userDao; }}?
布局代码如下
?
?
<?xml version="1.0" encoding="utf-8" ?> - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">- <LinearLayout android:layout_marginTop="80dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/username" android:layout_marginLeft="10dp" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入用户名" /> </LinearLayout>- <LinearLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码 " /> <EditText android:id="@+id/password" android:layout_marginLeft="10dp" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入密码" /> </LinearLayout>- <LinearLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> <Button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout> </LinearLayout>
?
?
?
<?xml version="1.0" encoding="utf-8" ?> - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">- <LinearLayout android:layout_marginTop="80dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名 " /> <EditText android:id="@+id/register_username" android:layout_marginLeft="10dp" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入用户名" /> </LinearLayout>- <LinearLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码 " /> <EditText android:id="@+id/register_password" android:layout_marginLeft="10dp" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="请输入密码" /> </LinearLayout>- <LinearLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重复密码" /> <EditText android:id="@+id/register_repassword" android:layout_marginLeft="10dp" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="重复密码" /> </LinearLayout>- <LinearLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/register_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout> </LinearLayout>
?
?
<?xml version="1.0" encoding="utf-8" ?> - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎登录" /> </LinearLayout>
?
当然工程里要加入ormlite的基础类包。
?
?
这样基于ormlite的登录注册的小例子就完成了。
?
1 楼 qzhai 2011-12-14
没有看到你的数据库,难道是用android内置的数据库,OrmliteLoginDemo.db这个是创建的数据库名称,如何可以使用SQLite expert查询到这个数据库的表数据呢
2 楼 348725767 2012-02-13
新建的OrmliteLoginDemo.db在data/data/包名 这路径下可以用exlipse将此文件导出。