我正在尝试创建一个应用程序,我有dbhelper with insert,每当我尝试登录我的应用程序时,它说我的数据库中没有项目,我不明白为什么,我正在努力超过一个月,请帮助。 我的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));
}
}
});
}
}
更新:
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 :(得分:0)
Key_User
字段为integer
和autoincrement
。您可以在String
方法中将Add
插入其中。您不应为此字段插入任何记录。数据库将管理此字段。你应该尝试以下方法。
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();
}