我正在尝试创建一个应用程序,我需要创建两个表寄存器n的详细信息。我希望注册用户可以登录,然后他们可以保存他们的详细信息。但任何人都可以告诉我如何在这两个表之间建立这种联系。我尝试这样做,但得到错误。
(PasswordKeeperDb.java)
public class PasswordKeeperDb {
private static final int DATABASE_VERSION = 1;
private DbHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
public static final String DATABASE_NAME = "PasswordKeeper";
private static final String REGISTER_TABLE_NAME = "RegisterTable";
private static final String DETAIL_TABLE_NAME = "DetailsTable";
private static String FIRST_NAME = "firstName", LAST_NAME = "lastName", EMAIL = "email",
PASSWORD = "password", LOGIN_ID = "loginId", DETAIL_ID = "detailsId", TITLE = "title";
private static final String REGISTER_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ REGISTER_TABLE_NAME
+ "("
+ LOGIN_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ FIRST_NAME
+ " VARCHAR(80) , "
+ LAST_NAME
+ " VARCHAR(80) , "
+ EMAIL
+ " VARCHAR(80) , "
+ PASSWORD
+ " VARCHAR(80) ) ";
private static final String DETAIL_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ DETAIL_TABLE_NAME
+ "("
+ DETAIL_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ LOGIN_ID
+ " INTEGER ,"
+ TITLE
+ " VARCHAR(80) , "
+ EMAIL
+ " VARCHAR(80) , "
+ PASSWORD
+ " VARCHAR(10) , )";
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(REGISTER_TABLE_CREATE);
db.execSQL(DETAIL_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public PasswordKeeperDb(Context c) {
dbHelper = new DbHelper(c);
}
public long insertRegisterTable(final RegisterModel registerModel)
throws SQLException {
ContentValues values = new ContentValues();
values.put(FIRST_NAME, registerModel.getFname());
values.put(LAST_NAME, registerModel.getLname());
values.put(EMAIL, registerModel.getEmail());
values.put(PASSWORD, registerModel.getPassword());
return sqLiteDatabase.insert(REGISTER_TABLE_NAME, null, values);
}
public long insertDetailTable(final AddModel addModel)
throws SQLException {
ContentValues values = new ContentValues();
values.put(LOGIN_ID, addModel.getLoginId());
values.put(TITLE, addModel.getTitle());
values.put(EMAIL, addModel.getEmail());
values.put(PASSWORD, addModel.getPassword());
return sqLiteDatabase.insert("DetailsTable", null, values);
}
public PasswordKeeperDb open() throws SQLException {
sqLiteDatabase = dbHelper.getWritableDatabase();
sqLiteDatabase = dbHelper.getReadableDatabase();
return this;
}
public Cursor getUser(String email, String pass) {
return sqLiteDatabase.rawQuery("select * from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
"' and password=" + "'" + pass + "'", null);
}
public Cursor getLoginId(String email) {
return sqLiteDatabase.rawQuery("select loginId from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
"'", null);
}
public void close() {
dbHelper.close();
}
}
(MainActivity.java)
public class MainActivity extends AppCompatActivity {
FloatingActionButton floatingActionButton;
ListView listView;
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
PasswordKeeperDb passwordKeeperDb;
View mView = getLayoutInflater().inflate(R.layout.dialog_details, null);
final EditText mEmail = (EditText) mView.findViewById(R.id.email);
final EditText mTitle = (EditText) mView.findViewById(R.id.title);
final EditText mPassword = (EditText) mView.findViewById(R.id.password);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
floatingActionButton = (FloatingActionButton) findViewById(R.id.add);
passwordKeeperDb = new PasswordKeeperDb(this);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
Button mAdd = (Button) mView.findViewById(R.id.dadd);
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTitle.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Add Title", Toast.LENGTH_SHORT).show();
} else if (mEmail.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Add Email", Toast.LENGTH_SHORT).show();
} else if (mPassword.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Add Password!", Toast.LENGTH_SHORT).show();
} else {
passwordKeeperDb.open();
AddModel addModel = new AddModel("", getLoginId(mEmail), mTitle.getText().toString(), mEmail.getText().toString(), mPassword.getText().toString());
Long result = passwordKeeperDb.insertDetailTable(addModel);
Log.d("ADB", result + "");
if (result > 0) {
Toast.makeText(MainActivity.this, "ADDED Successfully", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this, "Error Occurred !", Toast.LENGTH_SHORT).show();
}
passwordKeeperDb.close();
}
}
});
dialog.show();
}
});
}
private String getLoginId(EditText mEmail) {
return getLoginId(this.mEmail);
}
}
Log Cat:
FATAL EXCEPTION: main
Process: com.example.hope.passwordkeeper, PID: 26605
android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS DetailsTable(detailsId INTEGER PRIMARY KEY AUTOINCREMENT , loginId INTEGER ,title VARCHAR(80) , email VARCHAR(80) , password VARCHAR(10) , )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.hope.passwordkeeper.PasswordKeeperDb$DbHelper.onCreate(PasswordKeeperDb.java:69)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.hope.passwordkeeper.PasswordKeeperDb.open(PasswordKeeperDb.java:105)
at com.example.hope.passwordkeeper.HomeActivity$1$1.onClick(HomeActivity.java:51)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
答案 0 :(得分:2)
首先纠正您的 DETAIL_TABLE_CREATE
private static final String DETAIL_TABLE_CREATE =
"CREATE TABLE " + TABLE_TEAM + " ("
+ DETAIL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ LOGIN_ID + " INTEGER ,"
+ TITLE + " VARCHAR(80) ,"
+ EMAIL + " VARCHAR(80) ,"
+ PASSWORD + " VARCHAR(10)" + ")";
然后清理重建和卸载OLD应用并再次运行
答案 1 :(得分:0)
请找到正确的代码: -
public class PasswordKeeperDb {
private static final int DATABASE_VERSION = 1;
private DbHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
public static final String DATABASE_NAME = "PasswordKeeper";
private static final String REGISTER_TABLE_NAME = "RegisterTable";
private static final String DETAIL_TABLE_NAME = "DetailsTable";
private static String FIRST_NAME = "firstName", LAST_NAME = "lastName", EMAIL = "email",
PASSWORD = "password", LOGIN_ID = "loginId", DETAIL_ID = "detailsId", TITLE = "title";
private static final String REGISTER_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ REGISTER_TABLE_NAME
+ "("
+ LOGIN_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ FIRST_NAME
+ " VARCHAR(80) , "
+ LAST_NAME
+ " VARCHAR(80) , "
+ EMAIL
+ " VARCHAR(80) , "
+ PASSWORD
+ " VARCHAR(80) ) ";
private static final String DETAIL_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ DETAIL_TABLE_NAME
+ "("
+ DETAIL_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT , "
+ LOGIN_ID
+ " INTEGER ,"
+ TITLE
+ " VARCHAR(80) , "
+ EMAIL
+ " VARCHAR(80) , "
+ PASSWORD
+ " VARCHAR(10) )";
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(REGISTER_TABLE_CREATE);
db.execSQL(DETAIL_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public PasswordKeeperDb(Context c) {
dbHelper = new DbHelper(c);
}
public long insertRegisterTable(final RegisterModel registerModel)
throws SQLException {
ContentValues values = new ContentValues();
values.put(FIRST_NAME, registerModel.getFname());
values.put(LAST_NAME, registerModel.getLname());
values.put(EMAIL, registerModel.getEmail());
values.put(PASSWORD, registerModel.getPassword());
return sqLiteDatabase.insert(REGISTER_TABLE_NAME, null, values);
}
public long insertDetailTable(final AddModel addModel)
throws SQLException {
ContentValues values = new ContentValues();
values.put(LOGIN_ID, addModel.getLoginId());
values.put(TITLE, addModel.getTitle());
values.put(EMAIL, addModel.getEmail());
values.put(PASSWORD, addModel.getPassword());
return sqLiteDatabase.insert("DetailsTable", null, values);
}
public PasswordKeeperDb open() throws SQLException {
sqLiteDatabase = dbHelper.getWritableDatabase();
sqLiteDatabase = dbHelper.getReadableDatabase();
return this;
}
public Cursor getUser(String email, String pass) {
return sqLiteDatabase.rawQuery("select * from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
"' and password=" + "'" + pass + "'", null);
}
public Cursor getLoginId(String email) {
return sqLiteDatabase.rawQuery("select loginId from " + REGISTER_TABLE_NAME + " where email=" + "'" + email +
"'", null);
}
public void close() {
dbHelper.close();
}
您最后在创建明细表上添加了额外的逗号。