数据库。sqlite.sqliteexception;接近“存在类”:语法错误(代码1):在编译时:如果存在类,则删除表。

时间:2021-11-11 01:45:36

Blockquote when i run my application it's run normally, but when i click the button to add a user to the table,it's run "constructor called" normal but don't work OnUpgrade part code and saw me the error

当我运行我的应用程序时,它是正常运行的,但是当我点击按钮将用户添加到表中时,它运行的“构造函数”是正常的,但是不能在升级部分代码中工作,并且看到了错误。

"android.database.sqlite.SQLiteException;near "EXISTSclasses":syntax error(code 1): , while compiling:DROP TABLE IF EXISTSclasses"

“android.database.sqlite.SQLiteException”,接近“存在类”:语法错误(代码1):在编译时:如果存在类,则删除表。

public long insertData(String name, String password)
{
    SQLiteDatabase db=helper.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(DatabaseHelp.NAME, name);
    contentValues.put(DatabaseHelp.PASSWORD, password);
    long id=db.insert(DatabaseHelp.TABLE_NAME, null, contentValues);
    return id;
}
static class DatabaseHelp extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME="mydatabase";
    private static final String TABLE_NAME="classes";
    private static final String ID="_id";
    private static final String NAME="name";
    private static final String ADDRESS="address";
    private static final String PHONE="phone";
    private static final String PASSWORD="password";
    private static final int DATABASE_VERSION=160;
    private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" text not null,"+ADDRESS+" text not null,"+PHONE+" text not null,"+PASSWORD+" VARCHAR(255));";
    private static final String DROP_TABLE="DROP TABLE IF EXISTS"+TABLE_NAME;
    private Context context;

    public DatabaseHelp (Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
        this.context=context;
        Message.message(context, "constructor called");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
            try {
                db.execSQL(CREATE_TABLE);
            Message.message(context, "onCreate called");
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                Message.message(context, ""+e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        try {
            Message.message(context, "onUpgrade called");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            Message.message(context, ""+e);
        }
    }
}

enter code here

MainActivity.java, which creates the message Unsuccessfully and succesfull insert a row

MainActivity。java,它不成功地创建消息并成功地插入一行。

EditText username,password;
DatabaseAdaptor databaseHelpe;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    databaseHelpe=new DatabaseAdaptor(this);    


    username=(EditText) findViewById(R.id.UsernameValue);
    password=(EditText) findViewById(R.id.PasswordValue);
}

public void addUser(View view)
{
    String user=username.getText().toString();
    String pass=password.getText().toString();

    long id=databaseHelpe.insertData(user, pass);
    if(id<0)
    {
        Message.message(this,"Unsuccessful");
    }
    else
    {
        Message.message(this,"Successfully insert a row");
    }
}

2 个解决方案

#1


1  

You need a space here:

你需要一个空间:

DROP_TABLE="DROP TABLE IF EXISTS"+TABLE_NAME

Should be:

应该是:

DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME

#2


-1  

private static final  String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME +";";

Make sure the spaces correct or else it may cause errors.

确保空格正确,否则会造成错误。

#1


1  

You need a space here:

你需要一个空间:

DROP_TABLE="DROP TABLE IF EXISTS"+TABLE_NAME

Should be:

应该是:

DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME

#2


-1  

private static final  String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME +";";

Make sure the spaces correct or else it may cause errors.

确保空格正确,否则会造成错误。