由以下原因引起:android.database.sqlite.SQLiteException:编译时没有这样的列:名称(代码 1):

Caused by: android.database.sqlite.SQLiteException: no such column: name (code 1) while compiling:

我正在尝试制作一个注册页面,在该页面中我要求用户输入详细信息,然后将它们存储在数据库中,然后在单击 [=22= 时将它们打印在另一个 activity 中] 按钮仅通过数据库获取。

我的 MainActivity class 是:

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) 

改用此代码

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();
    }

查询失败,因为您没有对任何值进行转义,因此它假定它们是数据库中的列。

INSERT 查询应该类似于:

mydb.execSQL("INSERT INTO userInfo (Name, EmaiId , Password , CnfPassword ) VALUES ( \"name\" , \"emailId\" , \"password\" , \"cnfpassword\");")

mydb.execSQL("INSERT INTO userInfo (Name, EmaiId , Password , CnfPassword ) VALUES ( 'name' , 'emailId' , 'password' , 'cnfpassword');")

为避免所有转义,您也可以使用 ContentValues 方法。

您正在尝试插入这些变量吗?

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();

附加说明:请不要以明文形式存储密码。你为什么还要存储确认密码?