Create a Sqlite database (b)

Create a database table, and then delete the update operations in the main activity in

public class MainActivity extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Mysqlite mysqlite = new Mysqlite(this);
        SQLiteDatabase sqLiteDatabase = mysqlite.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put("id", 0);
        contentValues.put("name", "张三");
        sqLiteDatabase.insert("student", null, contentValues);
        System.out.println("插入数据");

        Cursor cursor = sqLiteDatabase.query("student", new String[]{"id", "name"}, "id=?", new String[]{"0"}, null,
                null, null);
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            System.out.println(id + "" + Name); 
        } 

            ContentValues contentValues1 = new new ContentValues (); 
            contentValues1.put ( "name", "John Doe" ); 
            sqLiteDatabase.update ( "? Student", contentValues1, "ID =", new new String [] { "0"}); // ? is a placeholder, behind String
         // element 0 of the array to fill the placeholder front, i.e. id = 0, whereClause (where words) only a limitation? 
            the System.out. println ( "update data" ); 

        sqLiteDatabase.delete ( "Student", "? ID =", new new String [] { "0" }); 
        System.out.println ( "Delete Data " ); 

        DeleteDatabase ("Student" ); 
        System.out.println ( "Delete Database" ); 

        sqLiteDatabase.close (); 
        } 
    }

The above is the main Activity, do not repeat create sqliteDatabase, otherwise can not synchronize the internal

public  class Mysqlite the extends SQLiteOpenHelper { 
    SQLiteDatabase readableDatabase; 
    public Mysqlite (Context context) {
         Super (context, "Student", null , 2 ); 
        readableDatabase = getWritableDatabase (); // create the database does not require any permission, but this line of code must be write 
    } 

    @Override 
    public  void the onCreate (the SQLiteDatabase DB) { 

        db.execSQL ( "Create Table Student (ID Integer, VARCHAR name)" ); 
        System.out.println ( "Create" ); 
    } 

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

    }
}

The above is to create a database // important things to say three times

Guess you like

Origin www.cnblogs.com/Ocean123123/p/10959019.html