Android 无法在注册表中注册(未插入表)
我已经尝试创建注册和登录页面。但是当我尝试注册时,字段值没有被插入。这是我的代码 MainActivity.javaAndroid 无法在注册表中注册(未插入表),android,android-layout,android-intent,android-activity,android-fragments,Android,Android Layout,Android Intent,Android Activity,Android Fragments,我已经尝试创建注册和登录页面。但是当我尝试注册时,字段值没有被插入。这是我的代码 MainActivity.java package com.example.login1; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.app.Activity; imp
package com.example.login1;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener
{
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText) findViewById(R.id.Ledituname);
mpassword = (EditText) findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if (username.equals("") || username == null) {
Toast.makeText(getApplicationContext(),
"Please enter User Name", Toast.LENGTH_SHORT).show();
} else if (password.equals("") || password == null) {
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else {
boolean validLogin = validateLogin(username, password,
getBaseContext());
if (validLogin) {
// System.out.println("In Valid");
//Intent in = new Intent(getBaseContext(), TabBar.class);
// in.putExtra("UserName", muname.getText().toString());
//startActivity(in);
Intent i1 = new Intent(getBaseContext(), Welcome.class);
Toast.makeText(getApplicationContext(),
"Success Valid Login", Toast.LENGTH_SHORT)
.show();
startActivity(i1);
// finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password,
Context baseContext) {
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = { "_id" };
String selection = "username=? AND password=?";
String[] selectionArgs = { username, password };
Cursor cursor = null;
try {
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match..\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
}
package com.example.loginexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 MainActivity extends Activity implements OnClickListener {
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText) findViewById(R.id.Ledituname);
mpassword = (EditText) findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if (username.equals("") || username == null) {
Toast.makeText(getApplicationContext(),
"Please enter User Name", Toast.LENGTH_SHORT).show();
} else if (password.equals("") || password == null) {
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else {
boolean validLogin = validateLogin(username, password,
getBaseContext());
if (validLogin) {
// System.out.println("In Valid");
//Intent in = new Intent(getBaseContext(), TabBar.class);
// in.putExtra("UserName", muname.getText().toString());
//startActivity(in);
Toast.makeText(getApplicationContext(),
"Success Valid Login", Toast.LENGTH_SHORT)
.show();
// finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password,
Context baseContext) {
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = { "_id" };
String selection = "username=? AND pass=?";
String[] selectionArgs = { username, password };
Cursor cursor = null;
try {
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match..\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
}
State.java
package com.example.login1;
public class State
{
public String name;
public State(String name)
{
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString()
{
return name;
}
}
Logcat
06-27 02:35:16.910: E/SQLiteLog(1393): (1) table igate1 has no column named password
06-27 02:35:16.930: E/SQLiteDatabase(1393): Error inserting gender=f lastname=f username=f firstname=f password=f email=f phoneno=f
06-27 02:35:16.930: E/SQLiteDatabase(1393): android.database.sqlite.SQLiteException: table igate1 has no column named password (code 1): , while compiling: INSERT INTO igate1(gender,lastname,username,firstname,password,email,phoneno) VALUES (?,?,?,?,?,?,?)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at com.example.login1.Registration.addEntry(Registration.java:215)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at com.example.login1.Registration.onClick(Registration.java:180)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.view.View.performClick(View.java:4438)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.view.View$PerformClick.run(View.java:18422)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.os.Handler.handleCallback(Handler.java:733)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.os.Handler.dispatchMessage(Handler.java:95)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.os.Looper.loop(Looper.java:136)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at java.lang.reflect.Method.invoke(Method.java:515)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-27 02:35:16.930: E/SQLiteDatabase(1393): at dalvik.system.NativeStart.main(Native Method)
错误清楚地表明:
表igate1没有名为password的列。
您的创建查询是:
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );";
还有pass
和cpass
字段,而不是password
改变这个,你就完了
希望这能有所帮助。logcat中的错误非常明显: SQLiteException:表igate1没有名为password的列 您将该字段声明为: 公共静态最终字符串键\u PASS=“PASS” 但您正试图在“密码”列中插入: 值。put(“密码”,password) 为什么不使用DBHelper中声明的列名常量?创建表字符串:
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );";
如果签出创建表字符串,则该字段称为pass
。但是,当您插入值时,您使用的是“密码”作为密钥。您需要确保在使用ContentValues向数据库中插入值时,键与表的列名相同
values.put("lastname", lname);
values.put("gender", Gen);
...
values.put("password", address);
此外,如果多次使用同一键输入值,则只保存最后一个值。因此,请确保不要使用同一个密钥两次
在您的情况下,您使用了两次
“password”
和“phoneno”
键。尽管这不会以崩溃的形式出现,但它可能会在以后引起关注 很可能您只是混淆了此行中的表列名
values.put("password", address);
可能是这样的
values.put("address", address);
检查其他列名。您还有其他打字错误(“密码”而不是“通行证”等)。
此外,列名还有字符串常量。使用它来防止此类错误
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FNAME +"TEXT NOT NULL, ....
及
嘿,你的代码有两个变化 第一个是您正在使用
String selection = "username=? AND password=?"; // change to pass
第二:字段名称错误:
ContentValues values = new ContentValues();
values.put("firstname", fname);
values.put("lastname", lname);
values.put("gender", Gen);
values.put("email", email);
values.put("username", phoneno);
values.put("password", address);
values.put("phoneno", bloodgroup);
values.put("lastname", dob);
values.put("gender", father);
values.put("email", mother);
values.put("username", username);
values.put("password", password);
values.put("phoneno", cpassword);
那里的用户有效字段。
这可能有助于您解决错误。您的问题是Registration.java-->addEntry()方法之间存在差异 和DB助手。如果答案回答了您的问题,请投票或标记答案 完成工作代码如下: MainActivity.java
package com.example.login1;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnClickListener
{
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText) findViewById(R.id.Ledituname);
mpassword = (EditText) findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if (username.equals("") || username == null) {
Toast.makeText(getApplicationContext(),
"Please enter User Name", Toast.LENGTH_SHORT).show();
} else if (password.equals("") || password == null) {
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else {
boolean validLogin = validateLogin(username, password,
getBaseContext());
if (validLogin) {
// System.out.println("In Valid");
//Intent in = new Intent(getBaseContext(), TabBar.class);
// in.putExtra("UserName", muname.getText().toString());
//startActivity(in);
Intent i1 = new Intent(getBaseContext(), Welcome.class);
Toast.makeText(getApplicationContext(),
"Success Valid Login", Toast.LENGTH_SHORT)
.show();
startActivity(i1);
// finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password,
Context baseContext) {
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = { "_id" };
String selection = "username=? AND password=?";
String[] selectionArgs = { username, password };
Cursor cursor = null;
try {
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match..\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
}
package com.example.loginexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 MainActivity extends Activity implements OnClickListener {
Button mLogin;
Button mRegister;
EditText muname;
EditText mpassword;
DBHelper DB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mRegister = (Button) findViewById(R.id.register);
mRegister.setOnClickListener(this);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.register:
Intent i = new Intent(getBaseContext(), Registration.class);
startActivity(i);
break;
case R.id.login:
muname = (EditText) findViewById(R.id.Ledituname);
mpassword = (EditText) findViewById(R.id.Leditpw);
String username = muname.getText().toString();
String password = mpassword.getText().toString();
if (username.equals("") || username == null) {
Toast.makeText(getApplicationContext(),
"Please enter User Name", Toast.LENGTH_SHORT).show();
} else if (password.equals("") || password == null) {
Toast.makeText(getApplicationContext(),
"Please enter your Password", Toast.LENGTH_SHORT)
.show();
} else {
boolean validLogin = validateLogin(username, password,
getBaseContext());
if (validLogin) {
// System.out.println("In Valid");
//Intent in = new Intent(getBaseContext(), TabBar.class);
// in.putExtra("UserName", muname.getText().toString());
//startActivity(in);
Toast.makeText(getApplicationContext(),
"Success Valid Login", Toast.LENGTH_SHORT)
.show();
// finish();
}
}
break;
}
}
private boolean validateLogin(String username, String password,
Context baseContext) {
DB = new DBHelper(getBaseContext());
SQLiteDatabase db = DB.getReadableDatabase();
String[] columns = { "_id" };
String selection = "username=? AND pass=?";
String[] selectionArgs = { username, password };
Cursor cursor = null;
try {
cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
startManagingCursor(cursor);
} catch (Exception e)
{
e.printStackTrace();
}
int numberOfRows = cursor.getCount();
if (numberOfRows <= 0) {
Toast.makeText(getApplicationContext(),
"User Name and Password miss match..\nPlease Try Again",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
return false;
}
return true;
}
public void onDestroy() {
super.onDestroy();
DB.close();
}
}
package com.example.login示例;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展活动实现OnClickListener{
按钮登录;
按钮注册器;
编辑文本名称;
编辑文本mpassword;
DBHelper DB=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
mRegister=(按钮)findviewbyd(R.id.register);
mRegister.setOnClickListener(此);
mLogin=(按钮)findviewbyd(R.id.login);
mLogin.setOnClickListener(这个);
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.登记册:
Intent i=新的Intent(getBaseContext(),Registration.class);
星触觉(i);
打破
案例R.id.login:
muname=(EditText)findViewById(R.id.Ledituname);
mpassword=(EditText)findViewById(R.id.Leditpw);
字符串username=muname.getText().toString();
字符串密码=mpassword.getText().toString();
if(username.equals(“”| | username==null){
Toast.makeText(getApplicationContext(),
“请输入用户名”,Toast.LENGTH_SHORT.show();
}else if(password.equals(“”| | password==null){
Toast.makeText(getApplicationContext(),
“请输入您的密码”,Toast.LENGTH\u SHORT)
.show();
}否则{
布尔值validLogin=validateLogin(用户名、密码、,
getBaseContext());
如果(有效登录){
//System.out.println(“In-Valid”);
//Intent in=newintent(getBaseContext(),TabBar.class);
//in.putExtra(“用户名”,muname.getText().toString());
//星触觉(in);
Toast.makeText(getApplicationContext(),
“成功有效登录”,Toast.LENGTH\u SHORT)
.show();
//完成();
}
}
打破
}
}
私有布尔validateLogin(字符串用户名、字符串密码、,
上下文基本上下文){
DB=newdbhelper(getBaseContext());
SQLiteDatabase db=db.getReadableDatabase();
字符串[]列={u id};
String selection=“username=”和pass=?”;
字符串[]selectionArgs={username,password};
游标=空;
试一试{
cursor=db.query(DBHelper.DATABASE\u TABLE\u名称、列、选择、,
selectionArgs,null,null,null);
开始管理游标(游标);
}捕获(例外e)
{
e、 printStackTrace();
}
int numberOfRows=cursor.getCount();
如果(行数arg0){
//TODO自动生成的方法存根
}
}
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/efname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<TextView
android:id="@+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/elname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"/>
<TextView
android:id="@+id/rgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:drawSelectorOnTop="true" />
<TextView
android:id="@+id/eusernameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/epassText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/epass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ecpassText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ecpass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ephnnoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ephnno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eaddText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ebloodGroupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blood Group"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ebloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/edobText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/edob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eFatherNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Father Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eFatherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eMotherNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mother Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eMotherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@id/submit"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
DBHelper.java
package com.example.login1;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.EditText;
import android.widget.Spinner;
public class DBHelper extends SQLiteOpenHelper
{
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONE = "phoneno";
public static final String KEY_ADDRESS = "address";
public static final String KEY_BLOODGROUP= "bloodgroup";
public static final String KEY_DOB= "dob";
public static final String KEY_FATHER= "father";
public static final String KEY_MOTHER= "mother";
public static final String KEY_USERNAME= "username";
public static final String KEY_PASS= "pass";
public static final String KEY_CPASS= "cpass";
DBHelper DB = null;
private static final String DATABASE_NAME = "igate.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "igate1";
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_TABLE_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE_NAME, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER,KEY_EMAIL,KEY_PHONE,KEY_ADDRESS,KEY_BLOODGROUP,
KEY_DOB,KEY_FATHER,KEY_MOTHER,KEY_USERNAME}, KEY_USERNAME + "=" + text, null, null, null, null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
package com.example.loginexample;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_USER = "username";
public static final String KEY_EMAIL = "email";
//New Fields
public static final String KEY_PHONE = "phoneno";
public static final String KEY_ADDRESS = "address";
public static final String KEY_BLOODGROUP= "bloodgroup";
public static final String KEY_DOB= "dob";
public static final String KEY_FATHER= "father";
public static final String KEY_MOTHER= "mother";
public static final String KEY_USERNAME= "username"; //Already Existing
public static final String KEY_PASS= "pass";
public static final String KEY_CPASS= "cpass";
DBHelper DB = null;
private static final String DATABASE_NAME = "srikanth1.db";
private static final int DATABASE_VERSION = 3;
public static final String DATABASE_TABLE_NAME = "sri1";
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_TABLE_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < newVersion)
{
db.execSQL("ALTER TABLE " + DATABASE_TABLE_NAME+ " RENAME TO "+DATABASE_TABLE_NAME+"_BK");
db.execSQL("CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );");
}
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE_NAME, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER,KEY_EMAIL,KEY_PHONE,KEY_ADDRESS,KEY_BLOODGROUP,
KEY_DOB,KEY_FATHER,KEY_MOTHER,KEY_USERNAME}, KEY_USERNAME + "=" + text, null, null, null, null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
package com.example.login示例;
导入android.cont
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/efname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<TextView
android:id="@+id/lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/elname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"/>
<TextView
android:id="@+id/rgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:drawSelectorOnTop="true" />
<TextView
android:id="@+id/eusernameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/epassText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/epass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ecpassText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm Password"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ecpass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ephnnoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ephnno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eaddText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/ebloodGroupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blood Group"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ebloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/edobText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/edob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eFatherNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Father Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eFatherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:id="@+id/eMotherNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mother Name"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/eMotherName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@id/submit"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
package com.example.loginexample;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_FNAME = "firstname";
public static final String KEY_LNAME = "lastname";
public static final String KEY_GENDER = "gender";
public static final String KEY_USER = "username";
public static final String KEY_EMAIL = "email";
//New Fields
public static final String KEY_PHONE = "phoneno";
public static final String KEY_ADDRESS = "address";
public static final String KEY_BLOODGROUP= "bloodgroup";
public static final String KEY_DOB= "dob";
public static final String KEY_FATHER= "father";
public static final String KEY_MOTHER= "mother";
public static final String KEY_USERNAME= "username"; //Already Existing
public static final String KEY_PASS= "pass";
public static final String KEY_CPASS= "cpass";
DBHelper DB = null;
private static final String DATABASE_NAME = "srikanth1.db";
private static final int DATABASE_VERSION = 3;
public static final String DATABASE_TABLE_NAME = "sri1";
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_TABLE_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < newVersion)
{
db.execSQL("ALTER TABLE " + DATABASE_TABLE_NAME+ " RENAME TO "+DATABASE_TABLE_NAME+"_BK");
db.execSQL("CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );");
}
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public void open() {
getWritableDatabase();
}
public Cursor getDetails(String text) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE_NAME, new String[] {
KEY_ROWID, KEY_FNAME, KEY_LNAME, KEY_GENDER,KEY_EMAIL,KEY_PHONE,KEY_ADDRESS,KEY_BLOODGROUP,
KEY_DOB,KEY_FATHER,KEY_MOTHER,KEY_USERNAME}, KEY_USERNAME + "=" + text, null, null, null, null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}