android 的简单 SQLite 代码

simple SQLite code for android

我正在尝试用 android 编写我的第一个 SQLite 数据库程序。一个简单的程序,按下一个按钮,我将一些数据添加到数据库中,然后按下另一个按钮从中读取数据,并用 toast 显示它。 不幸的是,该程序不起作用! 不知道是数据库没建好还是读取过程有问题。 该程序正在编译并生成 SPK 文件,但是当我按下按钮从数据库中读取时,我的平板电脑将挂起并退出该应用程序。 代码在这里:

public class MainActivity extends ActionBarActivity {
    Button btn1,btn2 ;
    bookBD db = new bookBD(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.bbtn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.AddBook("one",1,"the first comment");
                db.AddBook("two",2,"the second comment");
            }
        });
        btn2 = (Button) findViewById(R.id.bbtn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Cursor cr = (new) db.GetBook(1);
                Toast.makeText(MainActivity.this, cr.getString(cr.getColumnIndex(bookname)) , Toast.LENGTH_LONG).show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

数据库代码为:

class bookBD extends SQLiteOpenHelper {
    public static final String DB_name = "1sttry.db";
    public static final String Table_Name= "Bookdb";
    public static final String DB_id = "id";
    public static final String DB_bookname = "bookname";
    public static final String DB_price = "price";
    public static final String DB_content = "content";
    private static final String[] COLUMNS = {DB_id,DB_bookname,DB_price,DB_content};
    private static final String[] Columns = {DB_id,DB_bookname,DB_price,DB_content};
    public bookBD(Context context){
        super(context, DB_name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + Table_Name + "("
                + DB_id + " INTEGER PRIMARY KEY," + DB_bookname
                + " bookname," + DB_price + " INTEGER" + DB_content + " CONTENT" + ")";
        db.execSQL(CREATE_PRODUCTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS books"+Table_Name);
        this.onCreate(db);
    }

    public void AddBook(String Bname, int Bprice, String Bcontent){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_bookname,Bname);
        values.put(DB_price,Bprice);
        values.put(DB_content,Bcontent);
        db.insert(Table_Name,null,values);
        db.close();
    }
    public Cursor GetBook(int id){

        SQLiteDatabase db = this.getReadableDatabase();
    Cursor res ; 
        res =  db.rawQuery( "select * from contacts where id="+id+"", null );
    // or 
        //res = db.query (Table_Name,Columns, DB_id + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
        if (res != null)
            res.moveToFirst();
        return res;
    }
}

有人知道为什么我的代码不起作用吗?

改变

Cursor cr = (new) db.GetBook(1);

Cursor cr = db.GetBook(1);

同样在GetBook()方法中使用您评论的查询方法。

您在这里错过了 space:

+ " bookname," + DB_price + " INTEGER" + DB_content + " CONTENT" + ")";  

未创建 table。

试试这个:

+ " bookname," + DB_price + " INTEGER " + DB_content + " CONTENT" + ")";

也在这里:

db.execSQL("DROP TABLE IF EXISTS books"+Table_Name);

应该是:

db.execSQL("DROP TABLE IF EXISTS books "+Table_Name);