2017-03-02 56 views
0

我試圖做一個應用程序,我有插入dbhelper,每當我嘗試登錄到我的應用程序它說我沒有項目在我的數據庫中,我沒有得到它爲什麼,我正在爲此工作一個月,請幫忙。 我dbhelper數據庫一直說它沒有項目

package com.example.nir.nestleapp; 


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

import java.util.HashMap; 

public class MyDBHandler extends SQLiteOpenHelper { 
    public static final int DATABASE_VERSION = 3; 
    public static final String DATABASE_NAME = "UsersTable.db"; 
    public static final String Table_Name = "UsersTable"; 
    public static final String KEY_User = "User"; 
    public static final String KEY_Password = "Password"; 
    public static final String KEY_FullName = "FullName"; 
    public static final String KEY_PhoneNumber="PhoneNumber"; 
    public static final String KEY_IDNUMBER="IDNumber"; 
    public static final String[] DB_COL=new String[]{KEY_User, KEY_Password, KEY_FullName, KEY_PhoneNumber, KEY_IDNUMBER}; 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CreateTableSql = "CREATE TABLE " + Table_Name + " (" + 
       KEY_User + " INTEGER PRIMARY KEY AUTOINCREMENT , " + 
       KEY_Password + " TEXT , " + 
       KEY_FullName + " TEXT , "+ 
       KEY_PhoneNumber+" TEXT , "+ 
       KEY_IDNUMBER+ ") "; 
     db.execSQL(CreateTableSql); 
    }@Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + Table_Name); 
     onCreate(db); 
    } 

    public void Add(UserTable NewUser) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_User,NewUser.GetUserName()); 
     values.put(KEY_Password, NewUser.GetPassword()); 
     values.put(KEY_FullName,NewUser.GetFullName()); 
     values.put(KEY_PhoneNumber,NewUser.GetPhoneNumber()); 
     values.put(KEY_IDNUMBER,NewUser.GetID()); 
     db.insert(Table_Name, null, values); 
     db.close(); 
    } 
} 

這裏是我的登錄活動:

package com.example.nir.nestleapp; 

import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.opengl.EGLDisplay; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.lang.reflect.GenericArrayType; 
import java.util.ArrayList; 

import static com.example.nir.nestleapp.MyDBHandler.KEY_IDNUMBER; 

public class LoginActivity extends AppCompatActivity { 
    private SQLiteDatabase _database=null; 
    private MyDBHandler db=null; 
    Cursor c; 
    public DrawerLayout mDrawer; 
    public ActionBarDrawerToggle mToggle; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     db=new MyDBHandler(this); 
     _database=this.openOrCreateDatabase(db.DATABASE_NAME, MODE_PRIVATE,null); 
     SQLiteDatabase database=db.getReadableDatabase(); 
     c=database.query(db.Table_Name,db.DB_COL,null,null,null,null,null); 
     c.moveToFirst(); 
     mDrawer = (DrawerLayout) findViewById(R.id.activity_login); 
     mToggle = new ActionBarDrawerToggle(this, mDrawer, R.string.open, R.string.close); 
     mDrawer.addDrawerListener(mToggle); 
     mToggle.syncState(); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     final TextView RegisterPage = (TextView) findViewById(R.id.textView4); 
     TextView Text1 = (TextView) findViewById(R.id.textView3); 
     Button GuestLogin = (Button) findViewById(R.id.LoginGuestBtn); 
     RegisterPage.setTextSize(17); 
     Text1.setTextSize(17); 
     RegisterPage.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); 
      } 
     }); 
     GuestLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(LoginActivity.this, MainPageActivity.class)); 
      } 
     }); 
    } 
    public void LoginIntent(View view) { 
     EditText LoginUser = (EditText) findViewById(R.id.LoginUser); 
     EditText LoginPass = (EditText) findViewById(R.id.LoginPassword); 
     if (LoginUser.getText().toString().isEmpty() || LoginPass.getText().toString().isEmpty()) { 
      Toast.makeText(LoginActivity.this, "אנא מלא את כל הפרטים", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     c.moveToFirst(); 
     if (c.getCount() == 0) { 
      Toast.makeText(this, "NO ITEMS IN DB", Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     if (c.moveToFirst()) { 
      while (!c.isAfterLast()) { 
       if (c.getString(0).equals(LoginUser.getText().toString()) && c.getString(1).equals(LoginPass.getText().toString())) { 
        Toast.makeText(LoginActivity.this, "!", Toast.LENGTH_SHORT).show(); 
        startActivity(new Intent(LoginActivity.this, MainPageActivity.class)); 
        return; 
       } else c.moveToNext(); 
      } 
     } 
     Toast.makeText(LoginActivity.this, "!!", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.navigation_drawer, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     switch (item.getItemId()) { 
      case R.id.MainPage: 
       startActivity(new Intent(this, MainPageActivity.class)); 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


} 

這裏是我的註冊活動:

package com.example.nir.nestleapp; 

import android.content.Intent; 
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.TextView; 
import android.widget.Toast; 



public class RegisterActivity extends AppCompatActivity { 
    MyDBHandler db=new MyDBHandler(this); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 
     TextView RegisterHead = (TextView) findViewById(R.id.RegisterHeadline); 
     RegisterHead.setTextSize(25); 
     Button CompleteRegister = (Button) findViewById(R.id.CompleteRegisterBtn); 
     final EditText RegisteredUser = (EditText) findViewById(R.id.RegisteredUser); 
     final EditText RegisteredPass = (EditText) findViewById(R.id.RegisteredPass); 
     final EditText RegisteredName = (EditText) findViewById(R.id.RegisteredFn); 
     final EditText RegisteredPhone = (EditText) findViewById(R.id.RegisteredPhone); 
     final EditText RegisteredID = (EditText) findViewById(R.id.RegisteredID); 

     CompleteRegister.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       final String User2String = String.valueOf(RegisteredUser.getText()); 
       final String Pass2String = String.valueOf(RegisteredPass.getText()); 
       final String Name2String = String.valueOf(RegisteredName.getText()); 
       final String Phone2String = String.valueOf(RegisteredPhone.getText()); 
       String ID2String = String.valueOf(RegisteredID.getText()); 
       if (User2String.equals("") || Pass2String.equals("") || Name2String.equals("") || Phone2String.equals("") || ID2String.equals("")) { 
        Toast.makeText(RegisterActivity.this, "!!!", Toast.LENGTH_SHORT).show(); 
       } 
       else { 

        UserTable NewUser = new UserTable(User2String, Pass2String, Name2String, Phone2String, ID2String); 
        db.Add(NewUser); 
        Toast.makeText(RegisterActivity.this, "!", Toast.LENGTH_SHORT).show(); 
        startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); 
       } 
      } 
     }); 
    } 
} 

UPDATE:

package com.example.nir.nestleapp; 


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

import java.util.HashMap; 

public class MyDBHandler extends SQLiteOpenHelper { 
    public static final int DATABASE_VERSION = 3; 
    public static final String DATABASE_NAME = "UsersTable.db"; 
    public static final String Table_Name = "UsersTable"; 
    public static final String KEY_ID="ID"; 
    public static final String KEY_User = "User"; 
    public static final String KEY_Password = "Password"; 
    public static final String KEY_FullName = "FullName"; 
    public static final String KEY_PhoneNumber="PhoneNumber"; 
    public static final String KEY_IDNUMBER="IDNumber"; 
    public static final String[] DB_COL=new String[]{KEY_User, KEY_Password, KEY_FullName, KEY_PhoneNumber, KEY_IDNUMBER}; 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CreateTableSql = "Create Table " + Table_Name + " (" + 
       KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + 
       KEY_User + " TEXT , " + 
       KEY_Password + " TEXT , " + 
       KEY_FullName + " TEXT , "+ 
       KEY_PhoneNumber+" TEXT , "+ 
       KEY_IDNUMBER+ ") "; 
     db.execSQL(CreateTableSql); 
    }@Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + Table_Name); 
     onCreate(db); 
    } 
    public void Add(UserTable NewUser) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_User,NewUser.GetUserName()); 
     values.put(KEY_Password, NewUser.GetPassword()); 
     values.put(KEY_FullName,NewUser.GetFullName()); 
     values.put(KEY_PhoneNumber,NewUser.GetPhoneNumber()); 
     values.put(KEY_IDNUMBER,NewUser.GetID()); 
     db.insert(Table_Name, null, values); 
     db.close(); 
    } 
} 
+0

當你執行註冊時,它是否填充到數據庫? – Smit

回答

0

Key_User˚F現在是integerautoincrement。並在Add方法中插入String。您不應該爲此字段插入任何記錄。數據庫將管理這個領域。你應該嘗試下面的方法。

public void addUser(UserTable NewUser) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_Password, NewUser.getPassword()); 
     values.put(KEY_FullName,NewUser.getFullName()); 
     values.put(KEY_PhoneNumber,NewUser.getPhoneNumber()); 
     values.put(KEY_IDNUMBER,NewUser.getId()); 
     db.insert(Table_Name, null, values); 
     db.close(); 
    } 
+0

謝謝,現在它並沒有說數據庫是空的,但由於某種原因,當我嘗試登錄並檢查是否檢查用戶輸入的數據庫是否在數據庫中時,它無法找到並說明詳細信息是錯誤的 –

+0

看看 如果(0).equals(LoginUser.getText()。toString())&值。你正在與它比較。您應該爲名稱創建另一列,並嘗試與其進行比較。 – Derek

+0

所以如果生病添加一個id,這將是整數主鍵autoincrement,並生病檢查字符串.getstring(1)和.getstring(2)這應該工作? –

相關問題