我是android新手。我试图创建表,添加记录并检查下面给出的代码记录。问题是创建了名为TABLE_LOGIN的表以及表上的检查工作正常,但是没有按照Logcat中的说明创建第二个表TABLE_REG。
数据库助手:
package com.intraharyana.tamber.intraharyana;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME="NIC.db";
public static final String TABLE_LOGIN ="employee_login";
public static final String LOGIN_COL_1 ="USER_ID";
public static final String LOGIN_COL_2 ="PASSWORD";
public static final String TABLE_REG="employee_info";
public static final String REG_COL_1="SALARY_ACC";
public static final String REG_COL_2="DOB";
public static final String REG_COL_3="DOJ";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(LOGIN_COL_1, "admin");
contentValues.put(LOGIN_COL_2, "admin");
db.insert(TABLE_LOGIN, null, contentValues);
ContentValues contentValues1= new ContentValues();
contentValues1.put(REG_COL_1, "1234567890");
contentValues1.put(REG_COL_2, "28/01/1994");
contentValues1.put(REG_COL_3, "01/01/2016");
db.insert(TABLE_REG, null, contentValues1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_LOGIN + "(USER_ID VARCHAR PRIMARY KEY,PASSWORD TEXT)");
db.execSQL("create table " + TABLE_REG + "(SALARY_ACC VARCHAR PRIMARY KEY,DOB VARCHAR, DOJ VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+ TABLE_LOGIN);
db.execSQL("drop table if exists "+ TABLE_REG);
onCreate(db);
}
public Cursor checkLogin(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from "+ TABLE_LOGIN,null);
return cursor;
}
public Cursor checknewReg(){
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor1= db.rawQuery("select * from "+ TABLE_REG,null);
return cursor1;
}
}`
NewRegister Class
package com.intraharyana.tamber.intraharyana;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class NewRegister extends Activity {
DatabaseHelper mydb;
EditText emp_DOB,emp_DOJ,salaryacc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.newregister);
emp_DOB=(EditText)findViewById(R.id.emp_DOB);
emp_DOJ=(EditText)findViewById(R.id.emp_DOJ);
salaryacc=(EditText)findViewById(R.id.salaryacc);
mydb=new DatabaseHelper(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void checkReg(View view) {
String salary=salaryacc.getText().toString();
NewRegister ng= new NewRegister();
Cursor c = mydb.checknewReg();
int x=0;
if (c.moveToFirst()) {
do {
if (c.getString(0).equals(salaryacc.getText().toString().toLowerCase())
&& c.getString(1).equals(emp_DOB.getText().toString())
&& c.getString(2).equals(emp_DOJ.getText().toString())) {
Intent i = new Intent(this, NewRegister2.class);
startActivity(i);
finish();
x=1;
}
}
while (c.moveToNext());
if(x==0)
Toast.makeText(this,"Incorrect Credentials...Please Check again",Toast.LENGTH_LONG).show();
}
}
@Override
public void onBackPressed(){
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
finish();
}
}
logcat的
03-29 23:48:19.037 21421-21421/com.intraharyana.tamber.intraharyana E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.intraharyana.tamber.intraharyana, PID: 21421
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4240)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4235)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: android.database.sqlite.SQLiteException: no such table: employee_info (code 1): , while compiling: select * from employee_info
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1440)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1379)
at com.intraharyana.tamber.intraharyana.DatabaseHelper.checknewReg(DatabaseHelper.java:54)
at com.intraharyana.tamber.intraharyana.NewRegister.checkReg(NewRegister.java:151)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4235)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
答案 0 :(得分:0)
狂野猜测:您可能在上次运行后添加了第二张表 卸载您的应用并重新运行