I am trying to make a signUp page in which I am asking the user to enter the details and then I am storing them in database and then printing them in another activity on click of "Register Me" button by fetching through database only.
My MainActivity class is:
package com.example.hsports.signuppage;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String name;
String emailId;
String password;
String cnfpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.Title);
tv.setSelected(true);
}
public void storeInfo(View v) {
name=findViewById(R.id.name).toString();
emailId=findViewById(R.id.emailId).toString();
password=findViewById(R.id.password).toString();
cnfpassword=findViewById(R.id.cnfpassword).toString();
SQLiteDatabase mydb=openOrCreateDatabase("usersInfo",MODE_PRIVATE,null);
mydb.execSQL("create table IF NOT EXISTS userInfo ( Name varchar , EmaiId varchar , Password varchar , CnfPassword varchar );");
mydb.execSQL("insert into userInfo (Name, EmaiId , Password , CnfPassword ) Values( name , emailId , password , cnfpassword);");
Intent i=new Intent(this,SecondActivity.class);
startActivity(i);
}
}
My activity_main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hsports.signuppage.MainActivity"
android:orientation="vertical"
android:background="@color/WHITE">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shaadi Mubarak"
android:textSize="30dp"
android:textColor="@color/RED"
android:layout_gravity="center"
android:id="@+id/TitleText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="It is a company which aims at bringing people together and it's just a way to make this thing easier."
android:textSize="15dp"
android:textColor="@color/BLACK"
android:textAlignment="center"
android:paddingBottom="100dp"
android:id="@+id/Title"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:lines="1"
android:singleLine="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_gravity="center_horizontal"
android:hint="Enter your Name"
android:gravity="center_horizontal"
android:imeOptions="actionGo"
android:textColor="@color/BLACK" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="@+id/emailId"
android:hint="Enter your Email Id"
android:gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:hint="Enter Password"
android:gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/cnfpassword"
android:hint="Confirm your password"
android:gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RegisterMe"
android:textStyle="bold"
android:textSize="20dp"
android:padding="20dp"
android:onClick="storeInfo" />
</LinearLayout>
</LinearLayout>
the error which I am getting from the console is :
FATAL EXCEPTION: main
Process: com.example.hsports.signuppage, PID: 7232
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.database.sqlite.SQLiteException: no such column: name (code 1): , while compiling: insert into userInfo (Name, EmaiId , Password , CnfPassword ) Values( name , emailId , password , cnfpassword);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.hsports.signuppage.MainActivity.storeInfo(MainActivity.java:39)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method)
You are trying to insert these variables?
name=findViewById(R.id.name).toString();
emailId=findViewById(R.id.emailId).toString();
password=findViewById(R.id.password).toString();
cnfpassword=findViewById(R.id.cnfpassword).toString();
If those are TextViews, then you actually want to getText().toString()
instead
Then you need to put those values into the query rather than insert raw string values.
Definitely use ContentValues as well instead of raw SQL to prevent SQL injection. Also, I think column names are case sensitive.
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Name", name); // use the name variable, not the string "name" as the value
// etc.
db.insert("userInfo", null, values);
db.close();
Additional note: please do NOT store passwords in plaintext. Why do you even store the confirmation password?