且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLite的没有这种表错误

更新时间:2023-12-01 18:08:10

尝试递增的版本号:

 私有静态最终诠释版本= 2;

我猜你在某个时候改变了你的 CREATE TABLE 语句,但该系统将不会知道这些变化自动。更新您的模式,最简单的办法是加1 版本

同时

您不应该使用 execSQL()为插入命令,使用插入():

  ContentValues​​ initialValues​​ =新ContentValues​​();
initialValues​​.put(stbd.details,setdet);
initialValues​​.put(stbd.amt,setamt);
dobj.insert(stbd.tname,空,initialValues​​);

或者rawQuery():

 字符串insertQuery =INSERT INTO+ stdb.tname +(+ stdb.details +,+ stdb.amt +)VALUES(?,?);;
dobj.rawQuery(insertQuery,新的String [] {setdet,setamt});

I am getting a table not found error. I googled a lot but not able to found the source of the error

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);
    }

}

Try incrementing the version number:

private static final int version = 2;

I'm guessing that you changed your CREATE TABLE statement at some point, but the system won't know about these changes automatically. The easiest way to update your schema is to add 1 to version.

Also

You shouldn't use execSQL() for INSERT commands, use insert():

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

Or rawQuery():

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