嗨,我是Android的新手,这是我第一次使用sqlite,而我尝试添加或从数据库中读取我不断致命的例外:主要。
Signup.java
Signup.java
是主要活动调用的活动
public class Signup extends Activity
{
MySQLiteHelper db = new MySQLiteHelper(this);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
}
public void onClick(View v)
{
TextView user = (TextView) findViewById(R.id.EUser);
TextView pass = (TextView) findViewById(R.id.EPass);
TextView conpass = (TextView) findViewById(R.id.ConEPass);
String userid = user.getText().toString();
String password = pass.getText().toString();
String confirmpass = conpass.getText().toString();
if(userid.equals("") || password.equals("") || confirmpass.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter all fields",
Toast.LENGTH_LONG).show();
user.setText("");
pass.setText("");
conpass.setText("");
}
else if(!password.equals(confirmpass)){
Toast.makeText(getApplicationContext(), "Passwords don't match, please enter again",
Toast.LENGTH_LONG).show();
pass.setText("");
conpass.setText("");
}
else if(!db.getUserId(userid)){
Toast.makeText(getApplicationContext(), "user id alreddy exists, please enter a new one",
Toast.LENGTH_LONG).show();
user.setText("");
}
else{
Toast.makeText(getApplicationContext(), "Signing up ..............",
Toast.LENGTH_LONG).show();
db.insertUser(userid,password);
Intent i = new Intent(Signup.this, MainActivity.class);
startActivity(i);
}
}
}
MySQLiteHelper.java: -
public class MySQLiteHelper{
private static final String LOGCAT = null;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "User.db";
private static final String DATABASE_USERS = "Users";
private static final String KEY_USERID = "userid";
private static final String KEY_PASSWORD = "password";
private static final String[] COLUMNS = {KEY_USERID,KEY_PASSWORD};
private static final String TABLE_USERS = "create table "+DATABASE_USERS+" (userid TEXT, password TEXT)";
private SQLiteDatabase SQLdb;
private HelperClass helper;
private final Context mContext;
public MySQLiteHelper(Context context){
this.mContext=context;
helper = new HelperClass(mContext);
}
public class HelperClass extends SQLiteOpenHelper{
public HelperClass(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase SQLdb){
try{
SQLdb.execSQL(TABLE_USERS);
Log.d(LOGCAT, "Table Created");
}
catch(SQLException e){
}
}
@Override
public void onUpgrade(SQLiteDatabase SQLdb, int oldVersion, int newVersion){
SQLdb.execSQL("DROP TABLE IF EXISTS Users");
Log.d(LOGCAT,"Table being recreated");
onCreate(SQLdb);
}
}
public MySQLiteHelper open(){
Log.d(LOGCAT, "trying to open");
SQLdb = helper.getWritableDatabase();
return this;
}
public void close(){
helper.close();
}
public void insertUser(String Userid,String Password){
ContentValues values = new ContentValues();
values.put(KEY_USERID,Userid);
values.put(KEY_PASSWORD, Password);
open();
SQLdb.insert(DATABASE_USERS,null,values);
close();
Log.d(LOGCAT, "Inserted User");
}
public boolean getUserId(String userid){
open();
Cursor cursor = SQLdb.query(true,DATABASE_USERS,COLUMNS,KEY_USERID+"="+userid,null,null,null,null,null);//log shows as error
close();
if(cursor!=null){
Log.d(LOGCAT, "Found userid in DB");
return true;
}
else{
Log.d(LOGCAT, "Userid not found in DB");
return false;
}
}
public boolean getPassword(String password){
open();
Cursor cursor = SQLdb.query(true,DATABASE_USERS,COLUMNS,KEY_PASSWORD+"="+password,null,null,null,null,null);
if(cursor!=null){
Log.d(LOGCAT, "Found password in DB");
return true;
}
else{
Log.d(LOGCAT, "Password not found in DB");
return false;
}
}
}
这些是日志
01-24 16:46:19.024:E / SQLiteLog(12259):( 1)没有这样的列:lxnv 01-24 16:46:19.034:E / AndroidRuntime(12259):致命异常:主要 01-24 16:46:19.034:E / AndroidRuntime(12259):进程:com.lxnv.myfirstapp,PID:12259 01-24 16:46:19.034:E / AndroidRuntime(12259):java.lang.IllegalStateException:无法执行活动的方法 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.view.View $ 1.onClick(View.java:3823) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.view.View.performClick(View.java:4438) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.view.View $ PerformClick.run(View.java:18422) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.os.Handler.handleCallback(Handler.java:733) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.os.Handler.dispatchMessage(Handler.java:95) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.os.Looper.loop(Looper.java:136) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.app.ActivityThread.main(ActivityThread.java:5017) 01-24 16:46:19.034:E / AndroidRuntime(12259):at java.lang.reflect.Method.invokeNative(Native Method) 01-24 16:46:19.034:E / AndroidRuntime(12259):at java.lang.reflect.Method.invoke(Method.java:515) 01-24 16:46:19.034:E / AndroidRuntime(12259):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 01-24 16:46:19.034:E / AndroidRuntime(12259):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 01-24 16:46:19.034:E / AndroidRuntime(12259):at dalvik.system.NativeStart.main(Native Method) 01-24 16:46:19.034:E / AndroidRuntime(12259):引起:java.lang.reflect.InvocationTargetException 01-24 16:46:19.034:E / AndroidRuntime(12259):at java.lang.reflect.Method.invokeNative(Native Method) 01-24 16:46:19.034:E / AndroidRuntime(12259):at java.lang.reflect.Method.invoke(Method.java:515) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.view.View $ 1.onClick(View.java:3818) 01-24 16:46:19.034:E / AndroidRuntime(12259):......还有11个 01-24 16:46:19.034:E / AndroidRuntime(12259):引起:android.database.sqlite.SQLiteException:没有这样的列:lxnv(代码1):,同时编译:SELECT DISTINCT userid,password FROM Users WHERE userid = lxnv 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 01-24 16:46:19.034:E / AndroidRuntime(12259):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 01-24 16:46:19.034:E / AndroidRuntime(12259):at com.lxnv.myfirstapp.MySQLiteHelper.getUserId(MySQLiteHelper.java:76) 01-24 16:46:19.034:E / AndroidRuntime(12259):at com.lxnv.myfirstapp.Signup.onClick(Signup.java:43) 01-24 16:46:19.034:E / AndroidRuntime(12259):... 14更多
答案 0 :(得分:2)
MySQLiteHelper db = new MySQLiteHelper(null);
在此行,您无法将上下文传递为null
。
所以替换此
MySQLiteHelper db = new MySQLiteHelper(null);
有了这个
MySQLiteHelper db ;
并在OnCreate()
..
db = new MySQLiteHelper(this);
答案 1 :(得分:0)
尝试
SQLdb = getWritableDatabase();
在您的insertUser()方法中。