创建使用SQLite登录页面 [英] Creating Login page with SQLITE
本文介绍了创建使用SQLite登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用多列sqlite的创建登录页面,但无法在其中插入值:
这里是我的插入查询code:
包com.example.doctorsmanager;进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;公共类DataBaseAdapter
{
静态最后弦乐DATABASE_NAME =doctordb;
静态最终诠释DATABASE_VERSION = 2; 静态最后弦乐USERNAME_COLUMN =用户名;
静态最后弦乐PASSWORD_COLUMN =密码;
静态最后弦乐QUALIFICATION_COLUMN =资格;
静态最后弦乐SPECIALIZATION_COLUMN =专业化;
静态最后弦乐COLUMN_ID =ID;
静态最后弦乐REGISTRATIONNUMBER_COLUMN =RegistrationNumber;
静态最后弦乐CELLNUMBER_COLUMN =CellNumber;
// TODO:在您的表中每一列创建的公共领域。
// SQL语句创建一个新的数据库。
静态最后弦乐DATABASE_CREATE =创建表+LOGIN+
(+ID+整数主键自动增量,+USERNAME文本,密码字符串,字符串资格,专业化文字,REGISTRATIONNUMBER字符串,CELLNUMBER整数,字符串MAIL);
//公共无效的onCreate(SQLiteDatabase DB){
//字符串makeTable =CREATE TABLE+ TABLE_FOOD +(
// + NAME_COLUMN +TEXT+ CALORIE_COLUMN +INTEGER
// + PROTEIN_COLUMN +INTEGER+);
//变量来存放数据库实例
公共SQLiteDatabase分贝;
//使用该数据库的应用程序的语境。
私人最终上下文的背景下;
//数据库打开/升级助手
私人DataBaseHelper dbHelper;
公共DataBaseAdapter(上下文_context)
{
上下文= _context;
dbHelper =新DataBaseHelper(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
公共DataBaseAdapter的open()抛出的SQLException
{
DB = dbHelper.getWritableDatabase();
返回此;
}
公共无效的close()
{
db.close();
} 公共SQLiteDatabase getDatabaseInstance()
{
返回分贝;
} 公共无效insertEntry(用户名字符串,字符串密码,字符串资质,专业化的字符串,字符串registrationNumber,字符串cellNumber,字符串mailId)
{
ContentValues newValues =新ContentValues();
//指定值的每一行。
newValues.put(username的用户名);
newValues.put(密码,密码);
newValues.put(资格,资格);
newValues.put(专业化,专业化);
newValues.put(REGISTRATIONNUMBER,registrationNumber);
newValues.put(CONTACTNUMBER,cellNumber);
newValues.put(EMAIL,mailId); //将该行插入你的表
db.insert(登录,空,newValues); 的System.out.println(USERNAME);
的System.out.println(密码); // Toast.makeText(背景下,提醒保存成功,Toast.LENGTH_LONG).show();
}
公众诠释deleteEntry(用户名字符串)
{
//字符串ID =将String.valueOf(ID);
字符串,其中=USERNAME =?;
INT numberOFEntriesDeleted = db.delete(登录,在那里,新的String [] {用户名});
// Toast.makeText(背景下,数字FO项中删除成功:+ numberOFEntriesDeleted,Toast.LENGTH_LONG).show();
返回numberOFEntriesDeleted;
}
公共字符串getSinlgeEntry(用户名字符串)
{
光标光标= db.query(登录,NULL,USERNAME =?,新的String [] {用户名},NULL,NULL,NULL);
如果(cursor.getCount()&。1)//用户名不存在
{
cursor.close();
返回不存在;
}
cursor.moveToFirst();
字符串密码= cursor.getString(cursor.getColumnIndex(密码));
cursor.close();
返回密码;
}
公共无效updateEntry(用户名字符串,字符串密码,字符串资质,专业化的字符串,字符串registrationNumber,字符串cellNumber,字符串mailId)
{
//定义更新的行的内容。
ContentValues updatedValues =新ContentValues();
//指定值的每一行。
updatedValues.put(username的用户名);
updatedValues.put(密码,密码);
updatedValues.put(资格,资格);
updatedValues.put(专业化,专业化);
updatedValues.put(REGISTRATIONNUMBER,registrationNumber);
updatedValues.put(CONTACTNUMBER,cellNumber);
updatedValues.put(EMAIL,mailId); 字符串,其中=USERNAME =?;
db.update(登录,updatedValues,在那里,新的String [] {用户名});
}
}
这里是注册页面,当用户preSS的按钮
以创建帐户上的code:
btnCreateAccount.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){
// TODO自动生成方法存根 字符串username = editTextUserName.getText()的toString()。
。字符串密码= editTextPassword.getText()的toString();
。字符串资质= editTextQualification.getText()的toString();
。字符串specializaion = editTextSpecialization.getText()的toString();
。字符串registrationNumber = editTextRegistrationNumber.getText()的toString();
。字符串cellNumber = editTextCellNumber.getText()的toString();
。字符串mailId = editTextMail.getText()的toString(); //检查是否有字段是vaccant
如果(userName.equals()|| password.equals())
{
Toast.makeText(getApplicationContext(),现场Vaccant,Toast.LENGTH_LONG).show();
返回;
}
//检查两个密码匹配
如果(!password.equals(密码))
{
Toast.makeText(getApplicationContext(),密码不匹配,Toast.LENGTH_LONG).show();
返回;
}
其他
{
//将数据保存到数据库
DataBaseAdapter.insertEntry(userName,password,qualification,specializaion,registrationNumber,cellNumber,mailId);
Toast.makeText(getApplicationContext(),账户成功塑造了,Toast.LENGTH_LONG).show();
和这里是主要的登录页面登录成功查询:
btnLogIn.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//获取用户名和密码
字符串username = editTextUserName.getText()的toString()。
。字符串密码= editTextPassword.getText()的toString(); //为获取相应的用户名的密码形式数据库
字符串storedPassword = DataBaseAdapter.getSinlgeEntry(用户名); //检查存储的密码与密码匹配由用户输入
如果(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this,恭喜:登录全成,Toast.LENGTH_LONG).show();
// dialog.dismiss();
}`输入code here`
其他
{
Toast.makeText(MainActivity.this,用户名或密码不匹配,Toast.LENGTH_LONG).show();
}
//意图intentlogin =新意图(getApplicationContext(),LoginSuccess.class);
// startActivity(intentlogin);
}
});
这是我的数据库辅助类:
包com.example.doctorsmanager;进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;公共类DataBaseHelper扩展SQLiteOpenHelper
{
公共DataBaseHelper(上下文的背景下,字符串名称,CursorFactory工厂,INT版)
{
超(背景下,名称,厂家,版本);
}
//当盘和辅助类的需求不存在的数据库调用
//创建一个新的。
@覆盖
公共无效的onCreate(SQLiteDatabase _db)
{
_db.execSQL(DataBaseAdapter.DATABASE_CREATE); }
//当有一个数据库版本不匹配意味着该版本,称为
//在磁盘上的数据库需要升级到最新版本。
@覆盖
公共无效onUpgrade(SQLiteDatabase _db,诠释_oldVersion,诠释_newVersion)
{
//日志的版本升级。
Log.w(TaskDBAdapter,从版本升级+ _oldVersion +到+ _newVersion +,这将摧毁所有旧数据); //升级现有的数据库,以符合新的版本。多
// previous版本可以通过比较_oldVersion和_newVersion处理
//值。
//最简单的情况是删除旧表,并创建一个新的。
_db.execSQL(DROP TABLE IF EXISTS+模板);
//创建一个新的。
的onCreate(_db);
}}
这是日志的猫:
二月2日至28日:37:47.838:W / TaskDBAdapter(14302):升级从版本1到2,这将销毁所有旧数据
二月2日至28日:37:47.848:E / SQLiteLog(14302):(1)表LOGIN已经存在
二月2日至28日:37:47.908:D / AndroidRuntime(14302):关闭VM
二月2日至28日:37:47.908:W / dalvikvm(14302):主题ID = 1:螺纹未捕获的异常(组= 0xb4ab8ba8)退出
二月2日至28日:37:48.048:E / AndroidRuntime(14302):致命异常:主要
二月2日至28日:37:48.048:E / AndroidRuntime(14302):工艺:com.example.doctorsmanager,PID:14302
二月2日至28日:37:48.048:E / AndroidRuntime(14302):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.doctorsmanager / com.example.doctorsmanager.MainActivity}:android.database.sqlite.SQLiteException :表LOGIN已经存在(code 1),在编译:CREATE TABLE LOGIN(ID整数主键自动增量,USERNAME文本,密码字符串,字符串资格,专业化文字,REGISTRATIONNUMBER字符串,CELLNUMBER整数,字符串MAIL);
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.os.Handler.dispatchMessage(Handler.java:102)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.os.Looper.loop(Looper.java:136)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread.main(ActivityThread.java:5017)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在java.lang.reflect.Method.invokeNative(本机方法)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在java.lang.reflect.Method.invoke(Method.java:515)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在dalvik.system.NativeStart.main(本机方法)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):android.database.sqlite.SQLiteException:产生的原因表LOGIN已经存在(code 1),在编译:CREATE TABLE LOGIN(ID整数主键自动增量,USERNAME文本,密码字符串,字符串资格,专业化文字,REGISTRATIONNUMBER字符串,CELLNUMBER整数,字符串MAIL);
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteConnection.native prepareStatement(本机方法)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteConnection.acquire preparedStatement(SQLiteConnection.java:889)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteConnection prepare(SQLiteConnection.java:500)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteSession prepare(SQLiteSession.java:588)
。2月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteProgram<&初始化GT;(SQLiteProgram.java:58)
。2月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteStatement<&初始化GT;(SQLiteStatement.java:31)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.example.doctorsmanager.DataBaseHelper.onCreate(DataBaseHelper.java:20)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.example.doctorsmanager.DataBaseHelper.onUpgrade(DataBaseHelper.java:37)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.example.doctorsmanager.DataBaseAdapter.open(DataBaseAdapter.java:42)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在com.example.doctorsmanager.MainActivity.onCreate(MainActivity.java:29)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.Activity.performCreate(Activity.java:5231)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
二月2日至28日:37:48.048:E / AndroidRuntime(14302):... 11个
解决方案
// MainActivity.java
公共类MainActivity扩展ActionBarActivity {
按钮btlogin,btacc;
的EditText EMAILID,passwd文件;
SQLiteDatabase DB = NULL;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
btlogin =(按钮)findViewById(R.id.button1);
btacc =(按钮)findViewById(R.id.button2);
EMAILID =(EditText上)findViewById(R.id.editText1);
passwd文件=(EditText上)findViewById(R.id.editText2);
DB = openOrCreateDatabase(mydb的MODE_PRIVATE,NULL);
btacc.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
在意向=新意图(MainActivity.this,Signin.class);
startActivity(在);
}
});
btlogin.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){ // TODO自动生成方法存根
。字符串mail_id = emailid.getText()的toString();
字符串密码= passwd.getText()的toString()。
如果(mail_id ==空|| mail_id ==|| mail_id.length()小于10)
{
显示(请输入正确的邮件编号。);
}
否则如果(密码==空||密码==|| password.length()6;)
{
显示(请输入正确的密码。);
}
其他
{
光标C = db.rawQuery(从登录选择*其中EMAIL_ID ='+ mail_id +'和密码='+密码+',NULL);
c.moveToFirst();
如果(c.getCount()大于0)
{
意图I =新意图(MainActivity.this,Show.class);
i.putExtra(名,mail_id);
startActivityForResult(I,500); db.close();
完();
}
其他
剧(密码错误或电子邮件ID。); } } 私人无效秀(字符串字符串){
// TODO自动生成方法存根
Toast.makeText(getApplicationContext(),字符串Toast.LENGTH_LONG).show(); }
});
}
}
// Signin.java类
公共类签到扩展活动{
EditText上的ename,eemail,EMOBILE,enewpwd,erepwd;
按钮书;
SQLiteDatabase DB = NULL;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.signin);
为ename =(EditText上)findViewById(R.id.editText1);
eemail =(EditText上)findViewById(R.id.editText2);
EMOBILE =(EditText上)findViewById(R.id.editText3);
enewpwd =(EditText上)findViewById(R.id.editText4);
erepwd =(EditText上)findViewById(R.id.editText5);
书=(按钮)findViewById(R.id.button1);
DB = openOrCreateDatabase(mydb的MODE_PRIVATE,NULL);
db.execSQL(CREATE TABLE如果不存在登录(名称VARCHAR,VARCHAR EMAIL_ID,mobile_no VARCHAR,密码为varchar,确认密码为varchar)); bok.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
字符串名称= ename.getText()的toString()。
。字符串EMAIL_ID = eemail.getText()的toString();
。字符串mobile_no = emobile.getText()的toString();
。字符串密码= enewpwd.getText()的toString();
。字符串确认密码= erepwd.getText()的toString();
如果(名称== NULL ||名==|| name.length()3;)
{
显示(请输入正确的姓名。);
}
否则如果(mobile_no ==空|| mobile_no ==|| mobile_no.length()小于10)
{
显示(请输入正确的手机号码。);
}
否则,如果(EMAIL_ID == NULL || EMAIL_ID ==|| email_id.length()小于10)
{
显示(请输入正确的邮箱ID。);
}
否则如果(密码==空||密码==|| password.length()6;)
{
显示(请输入强密码。);
}
否则,如果(确认密码== NULL ||确认密码==)
{
显示(密码不匹配。);
}
其他
{
db.execSQL(插入登录值('+名字+,+ EMAIL_ID +,+ mobile_no +,+密码+,+确认密码+'));
在意向=新意图(Signin.this,Show.class);
in.putExtra(名,EMAIL_ID);
startActivity(在);
db.close();
完();
}
} 私人无效秀(字符串str){
// TODO自动生成方法存根
Toast.makeText(getApplicationContext(),STR,Toast.LENGTH_LONG).show();
}
});
}
}
//show.java类
公共类展会扩展活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.show);
意向意图= getIntent();
字符串n = intent.getStringExtra(名称);
TextView的T =(的TextView)findViewById(R.id.textView2);
字符串NN;
的String [] =分离n.split(@);
NN =分离[0];
//隔开[1];
t.setText(NN);
}
}
I am trying to create login page with sqlite with multiple columns but unable to insert values in it : Here is my insertion query code:
package com.example.doctorsmanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DataBaseAdapter
{
static final String DATABASE_NAME = "doctordb";
static final int DATABASE_VERSION = 2;
static final String USERNAME_COLUMN = "UserName";
static final String PASSWORD_COLUMN = "Password";
static final String QUALIFICATION_COLUMN = "Qualification";
static final String SPECIALIZATION_COLUMN = "Specialization";
static final String COLUMN_ID = "id";
static final String REGISTRATIONNUMBER_COLUMN = "RegistrationNumber";
static final String CELLNUMBER_COLUMN = "CellNumber";
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD String,QUALIFICATION String,SPECIALIZATION text,REGISTRATIONNUMBER String,CELLNUMBER integer,MAIL String); ";
// public void onCreate(SQLiteDatabase db){
// String makeTable = "CREATE TABLE " + TABLE_FOOD + "("
// + NAME_COLUMN + " TEXT," + CALORIE_COLUMN + " INTEGER,"
// + PROTEIN_COLUMN + " INTEGER" + ")";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public DataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password,String qualification,String specialization,String registrationNumber,String cellNumber,String mailId )
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("QUALIFICATION",qualification);
newValues.put("SPECIALIZATION",specialization);
newValues.put("REGISTRATIONNUMBER",registrationNumber);
newValues.put("CONTACTNUMBER",cellNumber);
newValues.put("EMAIL",mailId);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
System.out.println("USERNAME");
System.out.println("PASSWORD");
// Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSinlgeEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public void updateEntry(String userName,String password,String qualification,String specialization,String registrationNumber,String cellNumber,String mailId)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD",password);
updatedValues.put("QUALIFICATION",qualification);
updatedValues.put("SPECIALIZATION",specialization);
updatedValues.put("REGISTRATIONNUMBER",registrationNumber);
updatedValues.put("CONTACTNUMBER",cellNumber);
updatedValues.put("EMAIL",mailId);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
And here is the code on the signup page when user press the button
to create account:
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String qualification=editTextQualification.getText().toString();
String specializaion=editTextSpecialization.getText().toString();
String registrationNumber=editTextRegistrationNumber.getText().toString();
String cellNumber=editTextCellNumber.getText().toString();
String mailId=editTextMail.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(password))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
DataBaseAdapter.insertEntry(userName,password,qualification,specializaion,registrationNumber,cellNumber,mailId);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
and here is the query for main login page for successful login:
btnLogIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
// fetch the Password form database for respective user name
String storedPassword=DataBaseAdapter.getSinlgeEntry(userName);
// check if the Stored password matches with Password entered by user
if(password.equals(storedPassword))
{
Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
// dialog.dismiss();
}`enter code here`
else
{
Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
}
// Intent intentlogin=new Intent(getApplicationContext(),LoginSuccess.class);
// startActivity(intentlogin);
}
});
This is my database helper class:
package com.example.doctorsmanager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(DataBaseAdapter.DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// The simplest case is to drop the old table and create a new one.
_db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
// Create a new one.
onCreate(_db);
}
}
This is log cat:
02-28 02:37:47.838: W/TaskDBAdapter(14302): Upgrading from version 1 to 2, which will destroy all old data
02-28 02:37:47.848: E/SQLiteLog(14302): (1) table LOGIN already exists
02-28 02:37:47.908: D/AndroidRuntime(14302): Shutting down VM
02-28 02:37:47.908: W/dalvikvm(14302): threadid=1: thread exiting with uncaught exception (group=0xb4ab8ba8)
02-28 02:37:48.048: E/AndroidRuntime(14302): FATAL EXCEPTION: main
02-28 02:37:48.048: E/AndroidRuntime(14302): Process: com.example.doctorsmanager, PID: 14302
02-28 02:37:48.048: E/AndroidRuntime(14302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.doctorsmanager/com.example.doctorsmanager.MainActivity}: android.database.sqlite.SQLiteException: table LOGIN already exists (code 1): , while compiling: create table LOGIN( ID integer primary key autoincrement,USERNAME text,PASSWORD String,QUALIFICATION String,SPECIALIZATION text,REGISTRATIONNUMBER String,CELLNUMBER integer,MAIL String);
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.os.Handler.dispatchMessage(Handler.java:102)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.os.Looper.loop(Looper.java:136)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 02:37:48.048: E/AndroidRuntime(14302): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 02:37:48.048: E/AndroidRuntime(14302): at java.lang.reflect.Method.invoke(Method.java:515)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 02:37:48.048: E/AndroidRuntime(14302): at dalvik.system.NativeStart.main(Native Method)
02-28 02:37:48.048: E/AndroidRuntime(14302): Caused by: android.database.sqlite.SQLiteException: table LOGIN already exists (code 1): , while compiling: create table LOGIN( ID integer primary key autoincrement,USERNAME text,PASSWORD String,QUALIFICATION String,SPECIALIZATION text,REGISTRATIONNUMBER String,CELLNUMBER integer,MAIL String);
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.example.doctorsmanager.DataBaseHelper.onCreate(DataBaseHelper.java:20)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.example.doctorsmanager.DataBaseHelper.onUpgrade(DataBaseHelper.java:37)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.example.doctorsmanager.DataBaseAdapter.open(DataBaseAdapter.java:42)
02-28 02:37:48.048: E/AndroidRuntime(14302): at com.example.doctorsmanager.MainActivity.onCreate(MainActivity.java:29)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.Activity.performCreate(Activity.java:5231)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-28 02:37:48.048: E/AndroidRuntime(14302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-28 02:37:48.048: E/AndroidRuntime(14302): ... 11 more
解决方案
//MainActivity.java
public class MainActivity extends ActionBarActivity {
Button btlogin,btacc;
EditText emailid,passwd;
SQLiteDatabase db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btlogin=(Button)findViewById(R.id.button1);
btacc=(Button)findViewById(R.id.button2);
emailid=(EditText)findViewById(R.id.editText1);
passwd=(EditText)findViewById(R.id.editText2);
db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
btacc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in=new Intent(MainActivity.this,Signin.class);
startActivity(in);
}
});
btlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String mail_id=emailid.getText().toString();
String password=passwd.getText().toString();
if(mail_id==null||mail_id==""||mail_id.length()<10)
{
show("Please Enter Correct mail id.");
}
else if(password==null||password==""||password.length()<6)
{
show("Please Enter Correct Password.");
}
else
{
Cursor c=db.rawQuery("select * from login where email_id='"+mail_id+"' and password='"+password+"'",null);
c.moveToFirst();
if(c.getCount()>0)
{
Intent i=new Intent(MainActivity.this,Show.class);
i.putExtra("name",mail_id );
startActivityForResult(i,500);
db.close();
finish();
}
else
show("Wrong Password or Email Id.");
}
}
private void show(String string) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
}
});
}
}
//Signin.java class
public class Signin extends Activity {
EditText ename,eemail,emobile,enewpwd,erepwd;
Button bok;
SQLiteDatabase db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin);
ename=(EditText)findViewById(R.id.editText1);
eemail=(EditText)findViewById(R.id.editText2);
emobile=(EditText)findViewById(R.id.editText3);
enewpwd=(EditText)findViewById(R.id.editText4);
erepwd=(EditText)findViewById(R.id.editText5);
bok=(Button)findViewById(R.id.button1);
db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
db.execSQL("create table if not exists login(name varchar,email_id varchar,mobile_no varchar,password varchar,repassword varchar)");
bok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name=ename.getText().toString();
String email_id=eemail.getText().toString();
String mobile_no=emobile.getText().toString();
String password=enewpwd.getText().toString();
String repassword=erepwd.getText().toString();
if(name==null||name==""||name.length()<3)
{
show("Please Enter Correct Name.");
}
else if(mobile_no==null||mobile_no==""||mobile_no.length()<10)
{
show("Please Enter Correct mobile number.");
}
else if(email_id==null||email_id==""||email_id.length()<10)
{
show("Please Enter Correct Email id.");
}
else if(password==null||password==""||password.length()<6)
{
show("Please Enter Strong Password.");
}
else if(repassword==null||repassword=="")
{
show("password mismatch.");
}
else
{
db.execSQL("insert into login values('"+name+"','"+email_id+"','"+mobile_no+"','"+password+"','"+repassword+"')");
Intent in=new Intent(Signin.this,Show.class);
in.putExtra("name", email_id);
startActivity(in);
db.close();
finish();
}
}
private void show(String str) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}
});
}
} //show.java class
public class Show extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
Intent intent = getIntent();
String n = intent.getStringExtra("name");
TextView t=(TextView)findViewById(R.id.textView2);
String nn;
String[] separated = n.split("@");
nn=separated[0];
//separated[1];
t.setText(nn);
}
}
这篇关于创建使用SQLite登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文