未显示错误,但在数据库中插入失败

时间:2017-11-09 10:45:02

标签: android

以下是我的数据库的代码。在手机上运行时,不会显示错误,但会显示插入失败。

我的Register_Page.java如下:

    import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;


public class Register_Page extends AppCompatActivity {
    EditText Name, Pass , Email, Gender,Phone,Dob,City;
    no.nordicsemi.android.nrftoolbox.myDbAdapter helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register__page);
        Name= (EditText) findViewById(R.id.editText_name);
        Pass= (EditText) findViewById(R.id.editText_pass);
        Email= (EditText) findViewById(R.id.editText_email);
        Gender= (EditText) findViewById(R.id.editText_gender);
        Phone = (EditText) findViewById(R.id.editText_phone);
        Dob=(EditText)findViewById(R.id.editText_dob);
        City=(EditText)findViewById(R.id.editText_city);

        helper = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
    }
    public void addUser(View view)
    {
        String t1 = Name.getText().toString();
        String t2 = Email.getText().toString();
        String t3 = Pass.getText().toString();
        String t4 = Gender.getText().toString();
        String t5 = Phone.getText().toString();
        String t6 = Dob.getText().toString();
        String t7 = City.getText().toString();
        if(t1.isEmpty() || t2.isEmpty()|| t3.isEmpty() || t4.isEmpty() || t5.isEmpty() || t6.isEmpty() || t7.isEmpty())
        {
            no.nordicsemi.android.nrftoolbox.Message.message(getApplicationContext(),"Enter Both Name and Password");
        }
        else
        {
            long id = helper.insertData(t1,t2,t3,t4,t5,t6,t7);
            if(id<=0)
            {
                no.nordicsemi.android.nrftoolbox.Message.message(getApplicationContext(),"Insertion Unsuccessful");

            } else
            {
                no.nordicsemi.android.nrftoolbox.Message.message(getApplicationContext(),"Insertion Successful");

            }
        }
    }

    public void viewdata(View view)
    {
        String data = helper.getData();
        no.nordicsemi.android.nrftoolbox.Message.message(this,data);
    }

}

我的myDBHelper.java如下:

import android.app.Notification;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Message;



public class myDbAdapter {
    myDbHelper myhelper;
    public myDbAdapter(Context context)
    {
        myhelper = new myDbHelper(context);
    }

    public long insertData(String name, String email,String pass,String gender,String phone,String dob,String city)
    {
        SQLiteDatabase dbb = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME, name);
        contentValues.put(myDbHelper.MyPASSWORD, pass);
        contentValues.put(myDbHelper.EMAIlId, email);
        contentValues.put(myDbHelper.GENDER, gender);
        contentValues.put(myDbHelper.PHONE, phone);
        contentValues.put(myDbHelper.DOB, dob);
        contentValues.put(myDbHelper.CITY, city);
        long id = dbb.insert(myDbHelper.TABLE_NAME, null , contentValues);
        return id;
    }

    public String getData()
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        String[] columns = {myDbHelper.UID,myDbHelper.NAME,myDbHelper.MyPASSWORD};
        Cursor cursor =db.query(myDbHelper.TABLE_NAME,columns,null,null,null,null,null);
        StringBuffer buffer= new StringBuffer();
        while (cursor.moveToNext())
        {
            int cid =cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
            String name =cursor.getString(cursor.getColumnIndex(myDbHelper.NAME));
            String  password =cursor.getString(cursor.getColumnIndex(myDbHelper.MyPASSWORD));
            buffer.append(cid+ "   " + name + "   " + password +" \n");
        }
        return buffer.toString();
    }

    public  int delete(String uname)
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();


        int count=db.delete(myDbHelper.TABLE_NAME ,myDbHelper.NAME +" =? " ,new String[] {uname});
        return  count;
    }

    public int updateName(String oldName , String newName)
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME,newName);
        String[] whereArgs= {oldName};
        int count=db.update(myDbHelper.TABLE_NAME,contentValues, myDbHelper.NAME + "= ?",whereArgs );
        return count;
    }

    static class myDbHelper extends SQLiteOpenHelper
    {
        private static final String DATABASE_NAME = "myDatabase";    // Database Name
        private static final String TABLE_NAME = "myTable";   // Table Name
        private static final int DATABASE_Version = 1;    // Database Version
        private static final String UID="_id";     // Column I (Primary Key)
        private static final String NAME = "Name";    //Column II
        private static final String MyPASSWORD= "Password";// Column III
        private static final String EMAIlId= "Email-Id";
        private static final String GENDER= "Gender";
        private static final String PHONE= "Phone no.";
        private static final String DOB= "Date-Of-Birth";
        private static final String CITY= "City";

        private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                " NAME  TEXT NOT NULL,"+
                " EMAILId TEXT NOT NULL,"+
                " MyPASSWORD TEXT NOT NULL," +
                " GENDER TEXT NOT NULL,"+
                " PHONE INTEGER,"+
                " DOB INTEGER,"+
                " CITY TEXT NOT NULL)";
        private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
        private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context=context;
        }

        public void onCreate(SQLiteDatabase db) {

            try {
                db.execSQL(CREATE_TABLE);
            } catch (Exception e) {
                no.nordicsemi.android.nrftoolbox.Message.message(context,""+e);
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                no.nordicsemi.android.nrftoolbox.Message.message(context,"OnUpgrade");
                db.execSQL(DROP_TABLE);
                onCreate(db);
            }catch (Exception e) {
                no.nordicsemi.android.nrftoolbox.Message.message(context,""+e);
            }
        }
    }
}

我的activity_register_page.xml如下:

<RelativeLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical"
        android:weightSum="1">

        <TextView
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/editText_name"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="28dp"
            android:layout_weight="0.36"
            android:text="Register Here"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="#3333ff"
            android:textSize="28dp" />

        <TextView
            android:id="@+id/textView_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="70sp"
            android:autoText="false"
            android:text="Name :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="100sp"
            android:ems="10"
            android:hint="Enter your name"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="160sp"
            android:text="Email-id :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="190sp"
            android:ems="10"
            android:hint="Enter Email-id"
            android:inputType="textEmailAddress" />

        <TextView
            android:id="@+id/textView_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="250dp"
            android:hint="Enter Password"
            android:text="Password"
            android:textColor="#3368ff"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_pass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="280dp"
            android:ems="10"
            android:hint="Enter Password"
            android:inputType="textPassword" />

        <TextView
            android:id="@+id/textView_dob"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="450sp"
            android:text="Date of Birth :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_dob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="480dp"
            android:ems="10"
            android:hint="Date of Birth"
            android:inputType="date" />

        <TextView
            android:id="@+id/textView_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="540sp"
            android:text="Phone No. :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="570sp"
            android:ems="10"
            android:hint="Enter Phone No."
            android:inputType="phone" />

        <TextView
            android:id="@+id/textView_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="630sp"
            android:text="City :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_marginTop="660sp"
            android:ems="10"
            android:hint="Enter your City"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="14sp"
            android:layout_marginTop="340sp"
            android:text="Gender :"
            android:textColor="#3368ff"
            android:textColorLink="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/editText_gender"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="370dp"
            android:ems="10"
            android:hint="M/F/Others"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/button_register"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="720dp"
            android:background="#3368ff"
            android:onClick="addUser"
            android:text="Register"
            android:textAlignment="center"
            android:textColor="#ffffff" />

    </RelativeLayout>

代码有什么问题? 谁能解释并帮助纠正错误?

3 个答案:

答案 0 :(得分:0)

尝试将此代码替换为您的代码

if(id == -1)
        {
            no.nordicsemi.android.nrftoolbox.Message.message(getApplicationContext(),"Insertion Unsuccessful");

        } else
        {
            no.nordicsemi.android.nrftoolbox.Message.message(getApplicationContext(),"Insertion Successful");

        }

答案 1 :(得分:0)

在db helper中,您声明了列名的常量字段,并在插入方法中使用它们。在创建db时,您还应该使用这些值而不是硬编码值。

所以改变这个

 private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
            " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
            " NAME  TEXT NOT NULL,"+
            " EMAILId TEXT NOT NULL,"+
            " MyPASSWORD TEXT NOT NULL," +
            " GENDER TEXT NOT NULL,"+
            " PHONE INTEGER,"+
            " DOB INTEGER,"+
            " CITY TEXT NOT NULL)";

声明的常量字段

private static final String DATABASE_NAME = "myDatabase";    // Database Name
        private static final String TABLE_NAME = "myTable";   // Table Name
        private static final int DATABASE_Version = 1;    // Database Version
        private static final String UID="_id";     // Column I (Primary Key)
        private static final String NAME = "Name";    //Column II
        private static final String MyPASSWORD= "Password";// Column III
        private static final String EMAIlId= "Email-Id";
        private static final String GENDER= "Gender";
        private static final String PHONE= "Phone no.";
        private static final String DOB= "Date-Of-Birth";
        private static final String CITY= "City";

答案 2 :(得分:0)

首先,您需要在insertData之前执行调试步骤,并在

之前执行cursor.close()
return buffer.toString();
相关问题