Android应用程序中的数据库插入问题

时间:2019-01-21 13:13:59

标签: android sqlite

我试图在表Registeration中插入值。我的数据库名称是Demo。但是当我通过设备资源管理器而不是demo.db文件进行检查时,我仅获得了演示文件。我不知道问题是在数据库创建中还是在insertin中。请帮助我

//数据库类

public class dbClass extends SQLiteOpenHelper {

    private static final String DBNAME="DEMO";
    SQLiteDatabase db;
private static final String TBLE_NAME="Registeration";
    private static final String Column_1="FN";
    private static final String Column_2="LN";
    private static final String Column_3="UN";
    private static final String Column_4="PW";
    private static final String Column_5="EM";
    private static final String Column_6="PN";

    public dbClass(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DBNAME, null, 1);
                db=getWritableDatabase();
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        this.getWritableDatabase();
     String table="CREATE TABLE " +TBLE_NAME+ " (" +Column_1+ "TEXT, " +Column_2+ "TEXT," +Column_3+ "TEXT,"+Column_4+"TEXT,"+Column_5+"TEXT,"+Column_6+"TEXT"+");";
        db.execSQL(table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS "+TBLE_NAME);
 onCreate(db);

    }

    public boolean insert(String FN,String LN,String UN,String PW,String EM,String PN){

        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentvalues=new ContentValues();
        contentvalues.put("firstname",FN);
        contentvalues.put("lasttname",LN);
        contentvalues.put("username",UN);
        contentvalues.put("passtname",PW);
        contentvalues.put("email",EM);
       // contentvalues.put("phone",PN);
       long ins=0;

             ins = db.insert(TBLE_NAME, null, contentvalues);

       if(ins==-1)
       {
           return false;
       }
       else{

       return  true;
       }
    }


}

//活动分类

package com.example.dellpc.kitna_bacha;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends AppCompatActivity {

dbClass helper;

SQLiteDatabase db;
    EditText et1,et2,et3,et4,et5,et6,et7;
    Button btn;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        helper = new dbClass(this,"Demo",null,1);
        setContentView(R.layout.activity_sign_up);
        et1=(EditText)findViewById(R.id.editText3);
        et2=(EditText)findViewById(R.id.editText4);
        et3=(EditText)findViewById(R.id.editText5);
        et4=(EditText)findViewById(R.id.editText6);
        et5=(EditText)findViewById(R.id.editText7);
        et6=(EditText)findViewById(R.id.editText8);

        btn=(Button)findViewById(R.id.button2);
       btn.setOnClickListener(new View.OnClickListener(){

           @Override
           public void onClick(View v) {
           String s1=et1.getText().toString();
               String s2=et2.getText().toString();
               String s3=et3.getText().toString();
               String s4=et4.getText().toString();
               String s5=et5.getText().toString();
               String s6=et6.getText().toString();

           if(s3.equals("")||s4.equals(""))
           {
               Toast.makeText(SignUpActivity.this, "Username or Password cannot be empty", Toast.LENGTH_SHORT).show();
           }else {



                boolean insert=helper.insert(s1,s2,s3,s4,s5,s6);
                if(insert==true)
                {
                    Toast.makeText(SignUpActivity.this, "Registration Successfull!", Toast.LENGTH_SHORT).show();
                }

           }

           }
       });
    }


}

2 个答案:

答案 0 :(得分:0)

请将数据库名称DBNAME="DEMO"更改为DBNAME="DEMO.db",然后尝试

答案 1 :(得分:0)

表的Registeration列的名称为:

"FN", "LN", "UN", "PW", "EM", "PN"

但是在insert()方法中,您使用了不同的名称,例如:

"firstname", "lasttname", "username", "passtname", "email", "phone"

因此更改为此:

contentvalues.put("FN",FN);
contentvalues.put("LN",LN);
contentvalues.put("UN",UN);
contentvalues.put("PW",PW);
contentvalues.put("EM",EM);
contentvalues.put("PN",PN);

如果您创建的数据库名称为DEMO,那么很好,尽管通常SQLite数据库具有扩展名.db
如果要重新创建数据库,请从模拟器/设备上卸载应用程序并重新运行。
编辑
您在此行中缺少空格:

String table="CREATE TABLE " +TBLE_NAME+ " (" +Column_1+ "TEXT, " +Column_2+ "TEXT," +Column_3+ "TEXT,"+Column_4+"TEXT,"+Column_5+"TEXT,"+Column_6+"TEXT"+");";

更改为:

String table="CREATE TABLE " +TBLE_NAME+ " (" +Column_1+ " TEXT, " +Column_2+ " TEXT," +Column_3+ " TEXT,"+Column_4+" TEXT,"+Column_5+" TEXT,"+Column_6+" TEXT"+");";

卸载并重新运行该应用程序。