package com.example.databasestorage;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "Adj";
public static final int DB_VERSION = 1;
public static final String USER_DETAILS_TABLE = "user_details_table";
public static SQLiteDatabase db;
public static final String CREATE_USER_DETAILS_TABLE = " CREATE TABLE "
+ "user_details_table" + " (id INTEGER PRIMARY KEY AUTOINCREMENT"
+ "user_name TEXT NOT NULL " + "password TEXT NOT NULL"
+ "sex TEXT NOT NULL" + " email TEXT NOT NULL"
+ " remember_me TEXT ); ";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public static boolean DataBaseOpen() {
DBHelper dbHelper = new DBHelper(null);
db = dbHelper.getWritableDatabase();
return (db == null) ? false : true;
}
public void DataBaseClose() {
if (db != null)
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER_DETAILS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + USER_DETAILS_TABLE);
onCreate(db);
}
}
package com.adjetter.databasestorage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText userNameEditText;
EditText passwordEditText;
// EditText confirmPasswordEditTex;
EditText emailEditText;
TextView sex_textview;
String newPath;
Button submit;
CheckBox checkbox;
ImageButton facebookLogin;
ImageButton googleLogin;
View layout;
String picturePath = null;
private RadioGroup radioSexGroup;
SQLiteDatabase db;
DBHelper dbHelper;
String checkboxStatus = "false";
private RadioButton radioSexButton;
AlertDialog.Builder builder;
int selectedGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNameEditText = (EditText) findViewById(R.id.username);
passwordEditText = (EditText) findViewById(R.id.password);
emailEditText = (EditText) findViewById(R.id.email);
sex_textview = (TextView) findViewById(R.id.sex_textview);
submit = (Button) findViewById(R.id.submit_button);
radioSexGroup = (RadioGroup) findViewById(R.id.RG1);
checkbox = (CheckBox) findViewById(R.id.checkbox);
dbHelper = new DBHelper(this);
db = dbHelper.getWritableDatabase();
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectedGender = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedGender);
validate();
if(checkbox.isChecked()){
checkboxStatus = "true";
} else{
checkboxStatus = "false";
}
if(validate()){
// DBHelper.DataBaseOpen();
ContentValues cv = new ContentValues(5);
cv.put("user_name", userNameEditText.getText().toString());
cv.put("password", passwordEditText.getText().toString());
cv.put("sex", radioSexButton.getText().toString());
cv.put("email", emailEditText.getText().toString());
cv.put("remember_me", checkboxStatus);
db.insert(DBHelper.USER_DETAILS_TABLE, null, cv);
}
}
});
}
public boolean validate() {
Validation.isEmailAddress(emailEditText, true);
Validation.isUserName(userNameEditText, true);
Validation.isPassword(passwordEditText, true);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的:
10-09 13:41:49.807: E/SQLiteLog(16458): (1) near "AUTOINCREMENTuser_name": syntax error
10-09 13:41:49.824: E/AndroidRuntime(16458): FATAL EXCEPTION: main
10-09 13:41:49.824: E/AndroidRuntime(16458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adjetter.databasestorage/com.adjetter.databasestorage.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENTuser_name": syntax error (code 1): , while compiling: CREATE TABLE user_details_table (id INTEGER PRIMARY KEY AUTOINCREMENTuser_name TEXT NOT NULL password TEXT NOT NULLsex TEXT NOT NULL email TEXT NOT NULL remember_me TEXT );
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.os.Handler.dispatchMessage(Handler.java:107)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.os.Looper.loop(Looper.java:194)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-09 13:41:49.824: E/AndroidRuntime(16458): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 13:41:49.824: E/AndroidRuntime(16458): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 13:41:49.824: E/AndroidRuntime(16458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-09 13:41:49.824: E/AndroidRuntime(16458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-09 13:41:49.824: E/AndroidRuntime(16458): at dalvik.system.NativeStart.main(Native Method)
10-09 13:41:49.824: E/AndroidRuntime(16458): Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENTuser_name": syntax error (code 1): , while compiling: CREATE TABLE user_details_table (id INTEGER PRIMARY KEY AUTOINCREMENTuser_name TEXT NOT NULL password TEXT NOT NULLsex TEXT NOT NULL email TEXT NOT NULL remember_me TEXT );
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
10-09 13:41:49.824: E/AndroidRuntime(16458): at com.adjetter.databasestorage.DBHelper.onCreate(DBHelper.java:38)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-09 13:41:49.824: E/AndroidRuntime(16458): at com.adjetter.databasestorage.MainActivity.onCreate(MainActivity.java:60)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.Activity.performCreate(Activity.java:5122)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
10-09 13:41:49.824: E/AndroidRuntime(16458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
10-09 13:41:49.824: E/AndroidRuntime(16458): ... 11 more
答案 0 :(得分:0)
用逗号分隔值,例如
" (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "user_name TEXT NOT NULL, "
还在每个逗号后面设置一个空格。
答案 1 :(得分:0)
您需要使用逗号分隔列声明。
public static final String CREATE_USER_DETAILS_TABLE = " CREATE TABLE "
+ "user_details_table ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "user_name TEXT NOT NULL, "
+ "password TEXT NOT NULL, "
+ "sex TEXT NOT NULL, "
+ "email TEXT NOT NULL, "
+ "remember_me TEXT ); ";
P.S。:为了避免下次发生反击,您可能想要问一个问题。这比仅仅转储代码和日志更友好。