如何解决此SQLite异常?

时间:2019-03-24 08:48:26

标签: android sqlite

我已经在android的SQLite数据库中创建了一个表以进行跟踪和显示登录,我想在android studio的SQLite的同一数据库中创建另一个表

按用户表的注册和登录活动已成功运行,没有任何错误或异常。但是在创建第二个表人员时,它显示未找到此类表的异常

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper(Context context)
    {
        super(context, "Slab.db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("Create table IF NOT EXISTS user(userid integer Primary Key AutoIncrement,username varchar,password varchar)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
            db.execSQL("drop table if exists user");
            onCreate(db);
    }


    public boolean insert(String username, String password)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("username",username);
        contentValues.put("password",password);
        long ins=db.insert("user",null,contentValues);
        if (ins==-1)
            return false;
        else
            return true;
    }
    public boolean chkusername(String username)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from user where username=?",new String[]{username});
        if(cursor.getCount()>0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    public boolean chkusernamepassword(String username,String password)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from user where username=? and password=?",new String[]{username,password});
        if (cursor.getCount()>0)
        {
            return true;

        }
        else
        {
            return false;
        }



    }


Register.java

    b2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            String s1=e1.getText().toString();
            String s2=e2.getText().toString();
            String s3=e3.getText().toString();
            if(s1.isEmpty()||s2.isEmpty())
            {
                Toast.makeText(getApplicationContext(), "Plese Enter All Details", Toast.LENGTH_SHORT).show();
            }
            else
            {
                if(s2.equals(s3))
                {
                    Boolean chkusername=db.chkusername(s1);
                    if(chkusername==true)
                    {
                        Boolean insert=db.insert(s1,s2);
                        if(insert==true)
                        {
                            Toast.makeText(getApplicationContext(),"Registration Successful", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(),"Please Register Again",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Username Already Exists",Toast.LENGTH_SHORT).show();
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"Please Confirm Your Password", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });

StaffdatabaseHelper.java

public StaffDatabaseHelper(Context context)
    {
        super(context,"Slab.db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("Create table IF NOT EXISTS staff( staffid integer PRIMARY KEY AUTOINCREMENT,Name text,Email varchar,Phoneno integer,Companyname text,Salary integer,WorkingHours integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("drop table if exists staff");
        onCreate(db);
    }
    public boolean insert(String name,String email,String phoneno,String companyname,String salary,String wh)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("Name",name);
        contentValues.put("Email",email);
        contentValues.put("Phoneno",phoneno);
        contentValues.put("Companyname",companyname);
        contentValues.put("Salary",salary);
        contentValues.put("WorkingHours",wh);
        long ins=db.insert("staff",null,contentValues);
        if (ins==-1)
            return  false;
        else
            return true;
    }
    public boolean chkemailandchkphoneno(String email,String phoneno)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery("Select * from staff where Email=? and Phoneno=?",new String[]{email,phoneno});
       if(cursor.getCount()>0)
           return false;
       else
           return true;
    }

Staffform.java

staffDatabaseHelper=new StaffDatabaseHelper(this);
        b1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();
                String s3=e3.getText().toString();
                String s4=e4.getText().toString();
                String s5=e5.getText().toString();
                String s6=e6.getText().toString();
                if(s1.isEmpty()||s2.isEmpty()||s3.isEmpty()||s4.isEmpty()||s5.isEmpty()||s6.isEmpty())
                {
                    Toast.makeText(getApplicationContext(),"Please enter all details",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Boolean chkemailphone=staffDatabaseHelper.chkemailandchkphoneno(s2,s3);
                    if(chkemailphone==true)
                    {
                        Boolean insert=staffDatabaseHelper.insert(s1,s2,s3,s4,s5,s6);
                        if(insert==true)
                        {
                            Toast.makeText(getApplicationContext(),"Successfully added",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(staffform.this,staffdetails.class);
                            startActivity(intent);
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(),"Ooops!Please Try Again",Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Staff member already exists",Toast.LENGTH_SHORT).show();

                    }
                }

            }
        });

我成功添加了staffform的输出,但是输出是

E/SQLiteLog: (1) no such table: staff
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.slab, PID: 7639
    android.database.sqlite.SQLiteException: no such table: staff (code 1): , while compiling: Select * from staff where Email=? and Phoneno=?
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        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:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
        at com.example.slab.StaffDatabaseHelper.chkemailandchkphoneno(StaffDatabaseHelper.java:46)
        at com.example.slab.staffform$1.onClick(staffform.java:39)
        at android.view.View.performClick(View.java:6304)
        at android.view.View$PerformClick.run(View.java:24803)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

1 个答案:

答案 0 :(得分:1)

使用DatabaseHelper calss创建和存储整个应用程序中的每个表,如果您使用SQLiteOpenHelper,则android将仅为您的应用程序创建一个名称为eg(“ my_db”)的数据库,从而删除StaffDatabaseHelper类并执行操作在DatabaseHelper类中添加表创建查询到DatabaseHelper类的OnCreate方法中。