vendredi 26 juin 2015

About creat a simple SQLite Database

public class MyDataBaseHelper extends SQLiteOpenHelper{

    public static final String CREATE_BOOK = "creat table book ("
            + "id integer primary key autoincrement, "
            + "author text, "
            + "price real, "
            + "pages integer, "
            + "name text)";

    private Context mContext;

    public MyDataBaseHelper(Context context,String name,CursorFactory factory,int version)
    {   
        super(context ,name, factory, version );
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_BOOK);
        Toast.makeText(mContext,"Creat succeeded",Toast.LENGTH_SHORT).show();
    }

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


    }
}


public class MainActivity extends Activity {
    private MyDataBaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDataBaseHelper(this,"BookStore.db",null,1);
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();

            }
        });
    }
}


The sample just have a Button "create database".But when I clicked the Button ,it brokedown.I don't know what's wrong,I never learned SQL before,those codes above are from book.Did I miss something?Or what's wrong with my codes?Thanks.

Aucun commentaire:

Enregistrer un commentaire