проблема при входе в систему с базой данных SQLite, я создаю экран регистрации и экран входа в систему с базой данных SQlite. Вставка из экрана регистрации работает, данные вставляются успешно, но когда я пытаюсь получить данные из входа в базу данных, используя данные, ничего не ответило
Класс модели помощника SQlite Databse
public class SQLiteDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "info.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "login";
public static final String COLUMN_ID = "userid";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_SUR_NAME = "sur_name";
private static final String CREATE_TABLE_QUERY =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME+ " TEXT, " +
COLUMN_SUR_NAME + " TEXT " + ")";
//modified constructor
public SQLiteDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
Регистрационный XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#abc" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLines="1"
android:hint="Name"
android:layout_marginTop="28dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:hint="Sur Name"
android:maxLines="1"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Insert Values"
android:onClick="insert"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="display"
android:text="Display all Values" />
<Button
android:id="@+id/button3"
android:layout_below="@id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Login"
/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
Класс регистрации
public class MainActivity extends Activity {
SQLiteDatabase db;
TextView tv;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launch = new Intent(MainActivity.this,Login_page.class);
startActivity(launch);
}
});
tv=(TextView)findViewById(R.id.textView1);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
//create database if not already exist
db= openOrCreateDatabase("DATABASE_NAME", MODE_PRIVATE, null);
db.execSQL("create table if not exists TABLE_NAME(name varchar, sur_name varchar)");
}
private void startActivities(Intent intent) {
}
public void insert(View v)
{
String name=et1.getText().toString();
String sur_name=et2.getText().toString();
et1.setText("");
et2.setText("");
db.execSQL("insert into TABLE_NAME values('"+name+"','"+sur_name+"')");
Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
}
public void display(View v)
{
Cursor c=db.rawQuery("select * from TABLE_NAME", null);
tv.setText("");
c.moveToFirst();
do
{
String name=c.getString(c.getColumnIndex("name"));
String surname=c.getString(1);
tv.append("Name:"+name+" and SurName:"+surname+"\n");
}while(c.moveToNext());
}
}
Логин XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login_page">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Name"/>
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Surname"/>
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Login"/>
</LinearLayout>
</LinearLayout>
Класс входа
Когда я пытаюсь войти, используя базу данных, приложение перезапускается и перенаправляется на первый экран
public class Login_page extends AppCompatActivity {
SQLiteDatabase db1;
SQLiteOpenHelper dbhelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
final EditText ed11 = findViewById(R.id.ed1);
final EditText ed12 = findViewById(R.id.ed2);
Button btn1 = findViewById(R.id.btn_login);
dbhelper = new SQLiteDBHelper(this);
db1 = dbhelper.getReadableDatabase();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ed11.getText().toString();
String sur_name = ed12.getText().toString();
cursor = db1.rawQuery("SELECT *FROM "+SQLiteDBHelper.TABLE_NAME+" WHERE "+SQLiteDBHelper.COLUMN_NAME+"=? " + "AND "+SQLiteDBHelper.COLUMN_SUR_NAME+"=?",new String[] {name,sur_name});
if(cursor != null){
if (cursor.getCount() > 0){
cursor.moveToFirst();
String _name = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_NAME));
String _sur_name= cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_SUR_NAME));
Toast.makeText(Login_page.this, "Login Success", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login_page.this,new1.class);
intent.putExtra("name",_name);
intent.putExtra("sur_name",_sur_name);
startActivity(intent);
finish();
}
else {
Toast.makeText(Login_page.this,"Failed",Toast.LENGTH_LONG).show();
}
}
{
}
}
});
}
}