2017-10-15 155 views
-3

有代碼。我只是不知道是什麼問題。我只是將電子郵件和密碼作爲用戶的輸入並試圖存儲到名爲stu的表中。應用程序在點擊登錄按鈕時崩潰,該按鈕使用「intent」重定向到下一頁並將值插入到表中。 如果問題能夠被識別,導致應用程序崩潰,那將會非常有幫助。 感謝應用程序崩潰插入到SQLITE數據庫

數據庫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數據庫。我只是不知道什麼是錯的

+1

哪裏異常日誌? –

+0

讓我們來得到一個「沒有這樣的列」的例外? –

+0

找到解決方案。 Thankyou順便說一句:) –

回答

0

嘗試從調試設備上卸載現有的應用程序,然後再次安裝它。有時當你在安裝之後改變表格的結構,並且再次安裝時,它不會創建新的表格。

0

你應該試試這個...

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

    //add this line in your code.. 
    db = new DatabaseHelper(this); 

    et1= (EditText) findViewById(R.id.eTemail); 

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

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

這有幫助。 Thankyou –

+0

歡迎! @ZainAfzal – vivek

相關問題