Sqlite没有这样的表错误

时间:2012-07-29 19:38:23

标签: android sqlite

我找不到表格错误。我google了很多但没能找到错误的来源

public class update extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update);
        Button upbtn=(Button)findViewById(R.id.upbutton);
        final EditText detedit=(EditText)findViewById(R.id.upedit);
        final EditText amtedit=(EditText)findViewById(R.id.editText1);
        stdb obj=new stdb(this);
        final SQLiteDatabase dobj=obj.getWritableDatabase();
        upbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String setdet,setamt;
            setdet=detedit.getText().toString();
            setamt=amtedit.getText().toString();
            //try{

            String insertQuery = "INSERT INTO " +stdb.tname+" (" + stdb.details+" , "+stdb.amt+") VALUES("+setdet+" , "+setamt+");";

            //Log.e("sql",insertQuery);

            dobj.execSQL(insertQuery);
            //}
            //catch(SQLException e ){
            //  Log.e("sqlite",e.toString());

            //}

        }
    });


    }

}

SQLiteOpenHelper:

public class stdb extends SQLiteOpenHelper {

    private static final int version = 1;
    public static final String dname = "statments.db";
    public static final String tname="transactions";
    public static final String sid = "sno";
    public static final String details = "details";
    public static final String amt = "amount";

    public stdb(Context context) {
        super(context, dname, null, version);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String ex="CREATE TABLE " + tname + " (" + sid + " INTEGER PRIMARY KEY AUTOINCREMENT," + details+ " VARCHAR(255),"+amt+" INTEGER);";
        Log.e("ex",ex );

        db.execSQL("CREATE TABLE " + tname + " (" + sid + " INTEGER PRIMARY KEY AUTOINCREMENT," + details+ " VARCHAR(255),"+amt+" INTEGER);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS "+ tname);
        onCreate(db);
    }

}

1 个答案:

答案 0 :(得分:1)

尝试增加版本号:

private static final int version = 2;

我猜你在某个时候更改了CREATE TABLE语句,但系统不会自动知道这些更改。更新架构的最简单方法是向version添加1。

您不应将execSQL()用于INSERT命令,请使用insert():

ContentValues initialValues = new ContentValues();
initialValues.put(stbd.details, setdet);
initialValues.put(stbd.amt, setamt);
dobj.insert(stbd.tname, null, initialValues);

或者rawQuery():

String insertQuery = "INSERT INTO " +stdb.tname+" (" + stdb.details+" , "+stdb.amt+") VALUES(?, ?);";
dobj.rawQuery(insertQuery, new String[] {setdet, setamt});