我正在尝试创建一个signUp页面,其中我要求用户输入详细信息,然后我将它们存储在数据库中,然后在点击"注册我"通过仅通过数据库获取按钮。
我的MainActivity类是:
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);
}
}
我的activity_main.xml是:
<?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>
我从控制台获得的错误是:
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)
答案 0 :(得分:0)
您正在尝试插入这些变量?
name=findViewById(R.id.name).toString();
emailId=findViewById(R.id.emailId).toString();
password=findViewById(R.id.password).toString();
cnfpassword=findViewById(R.id.cnfpassword).toString();
如果这些是TextView,那么您实际上想要getText().toString()
而不是
然后,您需要将这些值放入查询中,而不是插入原始字符串值。
当然也要使用ContentValues而不是原始SQL来防止SQL注入。此外,我认为列名称区分大小写。
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();
附加说明:请勿以明文形式存储密码。为什么甚至存储确认密码?
答案 1 :(得分:-1)
尝试使用此代码
public void insertUserInfo () {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Name, "name");
values.put(Email, "email");
db.insert(LEAD_SOURCE_TABLE, null, values);
db.close();
}
答案 2 :(得分:-1)
查询失败,因为您没有转义任何值,因此它假定它们是数据库中的列。
INSERT
查询应该是:
mydb.execSQL("INSERT INTO userInfo (Name, EmaiId , Password , CnfPassword ) VALUES ( \"name\" , \"emailId\" , \"password\" , \"cnfpassword\");")
OR
mydb.execSQL("INSERT INTO userInfo (Name, EmaiId , Password , CnfPassword ) VALUES ( 'name' , 'emailId' , 'password' , 'cnfpassword');")
为了避免所有转义,您也可以使用ContentValues
方法。