android.database.sqlite.SQLiteDatatypeMismatchException:数据类型不匹配

时间:2017-04-13 19:20:41

标签: android login

我正在尝试使用SQlite创建一个简单的登录和注册应用程序。我在数据库中插入数据时收到错误:

  

android.database.sqlite.SQLiteDatatypeMismatchException:datatype mismatch

我的插入查询是否正确或是否需要对其进行更改?附加所有项目文件。任何帮助都非常感谢。

LoginActivity.java

  package com.example.sumeet.cardsharelogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    DataBaseHelper helper = new DataBaseHelper(this);
    EditText emaiId, pwd;
    String emailId_value, pwd_value;
    Button login_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login_btn = (Button) findViewById(R.id.login_btn);

        emaiId = (EditText) findViewById(R.id.emaiId_login);
        pwd = (EditText) findViewById(R.id.password_login);

        emailId_value = emaiId.getText().toString();
        pwd_value = pwd.getText().toString();


        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = helper.searchPassword(emailId_value);

                if (password.equals(pwd_value)) {
                    Intent intent = new Intent(getApplicationContext(), test.class);
                    startActivity(intent);

                } else {
                    Toast.makeText(getApplicationContext(), "Password Dont Match", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

RegistrationActivity.java

package com.example.sumeet.cardsharelogin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegistrationActivity extends AppCompatActivity {
    Button confirm;
    DataBaseHelper dataBaseHelper = new DataBaseHelper(this);
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(getApplicationContext(), "Passwords Dont Match", Toast.LENGTH_SHORT).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        final EditText name = (EditText) findViewById(R.id.name_reg);
        final EditText email_id = (EditText) findViewById(R.id.email_reg);
        final EditText password = (EditText) findViewById(R.id.pwd_reg);
        final EditText password_cnf = (EditText) findViewById(R.id.pwd_cnf);

        textView = (TextView) findViewById(R.id.acountexists_tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intent);
            }
        });

        confirm = (Button) findViewById(R.id.confirm_button);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name_value = name.getText().toString();
                String email_id_value = email_id.getText().toString();
                String password_value = password.getText().toString();
                String password_cnf_value = password_cnf.getText().toString();

                if (!password_value.equals(password_cnf_value)) {
                    Toast.makeText(getApplicationContext(), "Passwords Dont Match", Toast.LENGTH_SHORT).show();
                } else {
                    DataBaseStruture db = new DataBaseStruture();
                    db.setKey_Email(email_id_value);
                    db.setKey_name(name_value);
                    db.setKey_pwd(password_cnf_value);
                    dataBaseHelper.insertDetails(db);
                    Toast.makeText(getApplicationContext(), "user registered", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}

DataBaseHelper.java

package com.example.sumeet.cardsharelogin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Sumeet on 13-04-2017.
 */

public class DataBaseHelper extends SQLiteOpenHelper {


    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "CARDSHARE.db";
    private static final String TABLE_NAME = "Contacts";
    private static final String COLUMN_EMAIL_ID = "Email_Id";
    private static final String COLUMN_PASSWORD = "Password";
    private static final String COLUMN_NAME = "Name";
    SQLiteDatabase db;

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    private static final String TABLE_CREATE = "create table Contacts(Email_Id integer primary key not null,"
            + "Password text not null,Name text not null);";

    //  private static final String TABLE_CREATE1 = "CREATE TABLE" + TABLE_NAME + "(" + KEY_PRIMARY

    private static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME;


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db = db;

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

        db.execSQL(DROP_TABLE);
        this.onCreate(db);
    }

    public void insertDetails(DataBaseStruture c) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_EMAIL_ID, c.getKey_Email());
        values.put(COLUMN_PASSWORD, c.getKey_pwd());
        values.put(COLUMN_NAME, c.getKey_name());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public String searchPassword(String email_id) {
        db = this.getReadableDatabase();
        String query = "select Email_Id,Password from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        // return email_id;

        String email_id_fetched, password_fetched = "Not Found";
        if (cursor.moveToFirst()) {
            do {
                email_id_fetched = cursor.getString(0);
                password_fetched = cursor.getString(1);
                if (email_id_fetched.equals(email_id)) {
                    password_fetched = cursor.getString(1);
                    break;
                }
            } while (cursor.moveToNext());
        }
        return password_fetched;
    }
}

DataBaseStruture.java

package com.example.sumeet.cardsharelogin;

/**
 * Created by Sumeet on 13-04-2017.
 */

public class DataBaseStruture {

    String Key_Email, Key_name, Key_pwd;

    public DataBaseStruture() {
    }

    public DataBaseStruture(String key_Email, String key_name, String key_pwd) {
        this.Key_Email = key_Email;
        this.Key_name = key_name;
        this.Key_pwd = key_pwd;
    }

    public String getKey_Email() {
        return Key_Email;
    }

    public String getKey_name() {
        return Key_name;
    }

    public String getKey_pwd() {
        return Key_pwd;
    }

    public void setKey_Email(String key_Email) {
        Key_Email = key_Email;
    }

    public void setKey_name(String key_name) {
        Key_name = key_name;
    }

    public void setKey_pwd(String key_pwd) {
        Key_pwd = key_pwd;
    }

}

test.java

package com.example.sumeet.cardsharelogin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class test extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }
}

activity_login.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:layout_marginTop="40dp"
    android:orientation="vertical"
    tools:context="com.example.sumeet.cardsharelogin.LoginActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Enter Login Credentials"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <EditText
        android:id="@+id/emaiId_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:ems="10"
        android:hint="Enter Email ID"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/password_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter password"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/login_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:elevation="1dp"
        android:gravity="center_horizontal|center"
        android:text="Login" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="FORGOT PASSWORD" />

    <TextView
        android:id="@+id/dontHaveAccount_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Dont Have an Account ?" />

</LinearLayout>

activity_registration.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:layout_marginTop="50dp"
    android:orientation="vertical"
    tools:context="com.example.sumeet.cardsharelogin.RegistrationActivity">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Lets Get Started"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Please Enter the Information" />

    <EditText
        android:id="@+id/name_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/email_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email_ID"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/pwd_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/pwd_cnf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Confirm password"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/confirm_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:text="Confirm" />

    <TextView
        android:id="@+id/acountexists_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Alredy have an Account ?" />
</LinearLayout>

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.sumeet.cardsharelogin.test">

</android.support.constraint.ConstraintLayout>

logcat的

04-14 00:37:35.944 9706-9706/com.example.sumeet.cardsharelogin E/SQLiteLog: (20) statement aborts at 5: [INSERT INTO Contacts(Email_Id,Password,Name) VALUES (?,?,?)] datatype mismatch
04-14 00:37:35.945 9706-9706/com.example.sumeet.cardsharelogin E/SQLiteDatabase: Error inserting Email_Id=ee Password=pp Name=nn
                                                                                 android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
                                                                                     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                                                                                     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
                                                                                     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                                                                                     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                                                                                     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
                                                                                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                     at com.example.sumeet.cardsharelogin.DataBaseHelper.insertDetails(DataBaseHelper.java:57)
                                                                                     at com.example.sumeet.cardsharelogin.RegistrationActivity$2.onClick(RegistrationActivity.java:53)
                                                                                     at android.view.View.performClick(View.java:4791)
                                                                                     at android.view.View$PerformClick.run(View.java:19884)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
04-14 00:37:35.954 9706-9706/com.example.sumeet.cardsharelogin I/ViewRootImpl: CPU Rendering VSync enable = true

2 个答案:

答案 0 :(得分:1)

问题在这里你正在创建表:

create table Contacts(Email_Id integer primary key not null,

此处Email_IdInteger

您正在该列中插入string

 ContentValues values = new ContentValues();
 values.put(COLUMN_EMAIL_ID, c.getKey_Email()); //wrong

所以更改表格中Email_Id的数据类型。

使用:

private static final String TABLE_CREATE = "create table Contacts(Email_Id text primary key not null,"
        + "Password text not null,Name text not null);";

答案 1 :(得分:0)

"create table Contacts(Email_Id integer primary key not null,"

Email_Id 整数

在这里

                String email_id_value = email_id.getText().toString();
 DataBaseStruture db = new DataBaseStruture();
                    db.setKey_Email(email_id_value);
                    db.setKey_name(name_value);
                    db.setKey_pwd(password_cnf_value);
                    dataBaseHelper.insertDetails(db);

您想在Email_Id中插入字符串,这是整数