E / SQLiteLog:(1)没有这样的表:Class(Again)

时间:2017-12-11 12:40:27

标签: java android xml sqlite

最初我能够注册一个将其添加到数据库的用户,之后我能够使用登录表单验证注册用户,该表单检查了数据库中的用户电子邮件和密码,这项工作正确但是发生了一些事情和应用程序每次我点击“登录”按钮时都会崩溃。我知道有很多与此相关的问题,但我尝试过这些解决方案并没有成功。 谢谢您的帮助!

我的数据库处理代码:

public class DBHandler extends SQLiteOpenHelper {

public static final String DB__NAME = "ClubDB";
public static final int DB_VERSION=2;
public static final String TABLE_USER="User";
public static final String TABLE_CLUB="Club";

public static final String COL_ID="ID";
public static final String COL_ID2="ID";


public static final String COL_EMAIL="Email";
public static final String COL_PASSWORD="Password";


public static final String COL_CLUBNAME="ClubName";
public static final String COL_CLUBADDRESS="ClubAddress";
public static final String COL_X="X";
public static final String COL_Y="Y";
public static final String COL_TYPE="Type";



public static final String COL_NAME="Name";
public static final String COL_SURNAME="Surname";
public static final String COL_ADDRESS="Address";
public static final String COL_SPORT="SportType";
public static final String COL_MOBILE="Mobile";
public static final String COL_POSITION="Position";



public DBHandler (Context context){
    super(context, DB__NAME, null, DB_VERSION);
}

public void onCreate(SQLiteDatabase db){

    String sqlstatement = "CREATE TABLE " + TABLE_USER + "( " +
            COL_ID + " integer primary key autoincrement, " +
            COL_EMAIL + " text, " +
            COL_PASSWORD + " text, " +
            COL_NAME + " text, " +
            COL_SURNAME +  " text, " +
            COL_ADDRESS + " text, " +
            COL_SPORT + " text, " +
            COL_MOBILE + " text, " +
            COL_POSITION + " text ) ";

    String clubstatement = "CREATE TABLE " + TABLE_CLUB + "( " +
            COL_ID2 + " integer primary key autoincrement, " +
            COL_CLUBNAME + " text, " +
            COL_ADDRESS + " text, " +
            COL_X + " double, " +
            COL_Y + " double, " +
            COL_TYPE + " text ) ";



    db.execSQL(sqlstatement);




}

public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion)
{
    db.execSQL("drop table if exists " + TABLE_USER);
    db.execSQL("drop table if exists " + TABLE_CLUB);

    onCreate(db);
}

public boolean addUser(String email, String password, String name, String surname,
                       String address, String sport, String mobile, String position)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_EMAIL, email);
    contentValues.put(COL_PASSWORD, password);
    contentValues.put(COL_NAME, name);
    contentValues.put(COL_SURNAME, surname);
    contentValues.put(COL_ADDRESS, address);
    contentValues.put(COL_SPORT, sport);
    contentValues.put(COL_MOBILE, mobile);
    contentValues.put(COL_POSITION, position);





    long result = db.insert(TABLE_USER,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;




}

public boolean addClub(String Clubname, String Clubaddress, double x, double y, String type)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_CLUBNAME, Clubname);
    contentValues.put(COL_CLUBADDRESS, Clubaddress);
    contentValues.put(COL_X, x);
    contentValues.put(COL_Y, y);
    contentValues.put(COL_TYPE, type);


    long result = db.insert(TABLE_CLUB,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;

}
public void insert_User(String sqlInsert)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL( sqlInsert );
    db.close();
}

public void insert_Club(String sqlInsert)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL( sqlInsert );
    db.close();
}

public void update_User(String sqlUpdate){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL( sqlUpdate );
    db.close();
}

public void update_Club(String sqlUpdate){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL( sqlUpdate );
    db.close();
}


public ArrayList<User> selectAll_User( ){

    String sqlQuery = "select * from " + TABLE_USER;

    SQLiteDatabase db = this.getReadableDatabase( );
    Cursor cursor = db.rawQuery( sqlQuery, null);

    ArrayList<User> users = new ArrayList<User>( );
    while ( cursor.moveToNext( ) ) {
        User currentUser = new User(cursor.getInt( 0 ) ,
                cursor.getString( 1 ) , cursor.getString( 2 ) , cursor.getString( 3 ) ,
                cursor.getString( 4 ) , cursor.getString( 5 ) ,
                cursor.getString( 6 ) , cursor.getString( 7 ) , cursor.getString( 8 ) , cursor.getString( 9 ) ,
                cursor.getString( 10 ));
        users.add ( currentUser );
    }
    cursor.close();
    db.close( );
    return users;
}


public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_USER, null);
    return res;
}

public Cursor getAllClubData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_CLUB, null);
    return res;
}

}

错误代码:

12-11 12:26:32.248 2773-2773/com.example.c14378041.clubca2 E/SQLiteLog: (1) 
no such table: User
12-11 12:26:32.248 2773-2773/com.example.c14378041.clubca2 D/AndroidRuntime: 
Shutting down VM
12-11 12:26:32.250 2773-2773/com.example.c14378041.clubca2 E/AndroidRuntime: 
FATAL EXCEPTION: main      
Process: com.example.c14378041.clubca2, PID: 2773

java.lang.IllegalStateException: Could not execute method for 
android:onClick                                                                           
at 
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick 
(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)                                                                      
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429)                                                                                 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method)                                                                            
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                                        
Caused by: android.database.sqlite.SQLiteException: no such table: User 
(code 1): , while compiling: select * from User                                                                         
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 
Method)                                                                            
at 
  android.database.sqlite.SQLiteConnection.acquirePreparedStatement 
(SQLiteConnecti
 on.java:889)
at 
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
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:1318)
 at 
 android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at com.example.c14378041.clubca2.DBHandler.getAllData(DBHandler.java:188)
at com.example.c14378041.clubca2.MainActivity.Ulogin(MainActivity.java:45)
at java.lang.reflect.Method.invoke(Native Method) 
at 
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick 
     (Ap
       pCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119)                                                                              
at java.lang.reflect.Method.invoke(Native Method)                                                                           
at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
(ZygoteInit.java:886)                                                                           
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我调用数据库代码的Java方法:

public class MainActivity extends AppCompatActivity {
Button registerButton;
DBHandler dbhandler;
EditText email;
EditText password;
public static final String EXTRA_MESSAGE = "com.example.c14378041.clubca2";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button registerButton= (Button)findViewById(R.id.registerbutton);
   email = (EditText) findViewById(R.id.emaillogin);
    password = (EditText) findViewById(R.id.passwordlogin);
    dbhandler = new DBHandler(this);




}

public void adminLogin(View view)
{
    Intent intent = new Intent(this, Admin_Activity.class);
    startActivity(intent);
}

public void Ulogin(View view)
{


    Cursor cursor = dbhandler.getAllData();
    Boolean found = false;

    while (cursor.moveToNext()){

        if ( (cursor.getString(1).equals(email.getText().toString()) ) && (cursor.getString(2).equals(password.getText().toString())) )
        {
            Intent intent = new Intent(this, Welcome_Activity.class);

            String message = email.getText().toString();
            intent.putExtra(EXTRA_MESSAGE, message);
            startActivity(intent);
            found = true;
            Toast.makeText(MainActivity.this,"User Logged in!",Toast.LENGTH_SHORT).show();

        }
    }

    if ((cursor.isAfterLast()) && !found)
    {
        Toast.makeText(MainActivity.this,"Username or Password incorrect",Toast.LENGTH_SHORT).show();
        cursor.close();
    }



}

我的activity_main xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.c14378041.clubca2.MainActivity">

    <EditText
        android:id="@+id/passwordlogin"
        android:layout_width="256dp"
        android:layout_height="37dp"
        android:ems="10"
        android:hint="password"
        android:inputType="textPassword"
        android:textSize="15dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="222dp"
        android:layout_marginRight="64dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/Login"
        android:layout_width="224dp"
        android:layout_height="31dp"
        android:text="Already a User? Login:"
        android:textSize="20dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="30dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.409"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.031" />

    <TextView
        android:id="@+id/newUserView"
        android:layout_width="224dp"
        android:layout_height="31dp"
        android:text="New User? Register:"
        android:textSize="20dp"
        android:layout_marginLeft="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="262dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.588"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.371" />

    <TextView
        android:layout_width="337dp"
        android:layout_height="50dp"
        android:text="Find YOUR club"
        android:textSize="40dp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="361dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="1.0"
        android:id="@+id/textView3" />

    <Button
        android:id="@+id/loginbutton"
        android:layout_width="100dp"
        android:layout_height="36dp"
        android:text="Login"
        android:layout_marginTop="25dp"
        app:layout_constraintTop_toBottomOf="@+id/Login"
        android:layout_marginLeft="142dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.28"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        android:onClick="Ulogin"/>

    <EditText
        android:id="@+id/emaillogin"
        android:layout_width="256dp"
        android:layout_height="37dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Email@gmail.com"
        android:textSize="15dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="169dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.006"
        android:layout_marginLeft="30dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.377" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="143dp"
        android:layout_height="36dp"
        android:text="Register"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="364dp"
        android:layout_marginLeft="121dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.184"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        android:onClick="registerUser"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="70dp"
        android:layout_height="59dp"
        app:srcCompat="@mipmap/clubicon"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="45dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.045" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="39dp"
        android:layout_height="36dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:srcCompat="@mipmap/ic_launcher_round"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintVertical_bias="1.0"
        android:onClick="adminLogin"/>




    </android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

在onCreate()DHelper中添加db.execSQL(sqlstatement);,如下所示

    public void onCreate(SQLiteDatabase db){

    String sqlstatement = "CREATE TABLE " + TABLE_USER + "( " +
            COL_ID + " integer primary key autoincrement, " +
            COL_EMAIL + " text, " +
            COL_PASSWORD + " text, " +
            COL_NAME + " text, " +
            COL_SURNAME +  " text, " +
            COL_ADDRESS + " text, " +
            COL_SPORT + " text, " +
            COL_MOBILE + " text, " +
            COL_POSITION + " text ) ";

       db.execSQL(sqlstatement);

}

还更新DB version