应用程序在插入SQLITE数据库时崩溃

时间:2017-10-15 11:43:26

标签: android sql database sqlite android-sqlite

有代码。我只是想不出是什么问题。我只是将电子邮件和密码作为用户输入并尝试存储到名为stu的表中。应用程序在单击登录按钮时崩溃,该按钮使用“intent”重定向到下一页并将值插入表中。 如果可以确定导致应用程序崩溃的问题,将会有很大帮助。 感谢

Database Helper.java

package com.example.zain.smd1;



import android.content.ContentValues;

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





public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "student.db";
public static final String Table_name = "stu";
public static final String COL1 = "Id";
public static final String COL2 = "EMAIL";
public static final String COL3 = "PASS";
private static final String PASS = "PASS";
private static final String EMAIL = "EMAIL";

public DatabaseHelper(Context context) {
    super(context,DB_NAME,null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {


    db.execSQL("create table " + Table_name + " (Id INTEGER PRIMARY KEY AUTOINCREMENT, EMAIL TEXT , PASS TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+Table_name);
    onCreate(db);
}
public boolean insertdata(String email , String pass){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues content = new ContentValues();

    content.put(EMAIL,email);
    content.put(PASS,pass);
    long result = db.insert(Table_name,null,content);
    if (result==-1){
        return false;


    }
    else
        return true;

}
}

主要Activity.java

package com.example.zain.smd1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText et1,et2;
String email,pass;
RelativeLayout r1;
SharedPreferences sp;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     et1= (EditText) findViewById(R.id.eTemail);

     et2 = (EditText) findViewById(R.id.eTpassword);

    r1 = (RelativeLayout) findViewById(R.id.colorChange);
}

 public void login(View view){

     email = et1.getText().toString();
     pass = et2.getText().toString();
     if(email.equals("admin")&& pass.equals("admin")){
         Toast.makeText(getApplicationContext(),
                 "Redirecting...",Toast.LENGTH_SHORT).show();
         r1.setBackgroundColor(Color.RED);
          /*sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         SharedPreferences.Editor e= sp.edit();
         e.putString("name",email);
         e.putString("password",pass);
         e.apply();*/
         boolean ischecked = db.insertdata(et1.getText().toString(),et2.getText().toString());
         if(ischecked==true) {

             Toast.makeText(getApplicationContext(),
                     "Added",Toast.LENGTH_SHORT).show();
             Intent intent = new Intent(this, Main2Activity.class);

             startActivity(intent);
         }
     }
     else {
         r1.setBackgroundColor(Color.GREEN);
         Toast.makeText(getApplicationContext(),
                 "Invalid password or email",Toast.LENGTH_SHORT).show();


     }

    /*
     String name=sp.getString("name","");
     String pass= sp.getString("password","");
     et1.setText(name);
     et2.setText(pass);
     */
     }



     }

应用程序在插入SQLite数据库时崩溃。我无法弄清楚什么是错的

2 个答案:

答案 0 :(得分:0)

尝试从调试设备卸载现有应用程序,然后重新安装。有时当您在安装表之后更改表的结构并再次安装时,它不会创建新表。

答案 1 :(得分:0)

你应该试试这个......

class RequisitionLineForm(forms.ModelForm):

    sub_total = forms.DecimalField(disabled=True, required=False)

    class Meta:
        model = models.RequisitionLine
        fields = ['product', 'quantity', 'price_estimate', 'sub_total']