Create a SQLite database (2)

First create a database table, and then perform the delete update operation in the main activity

public< /span> 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(
"Insert data");

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 ContentValues();
contentValues1.put(
"name", "李四");
sqLiteDatabase.update(
"student", contentValues1, "id=?", new String[]{"0"}) ;//? is a placeholder, followed by String
// Element 0 of the array fills the placeholder in front?, That is, id=0, whereClause (where clause) is only a restriction condition
System.out.println("Update data");

sqLiteDatabase.delete(
"student", "id=?", new String[]{"0"});
System.out.println(
"Delete data");

deleteDatabase(
"student");
System.out.println(
"Delete database");

sqLiteDatabase.close();
}
}

The above is the main activity

public class Mysqlite extends SQLiteOpenHelper {

SQLiteDatabase readableDatabase;
public Mysqlite(Context context) {
super(context, "student", null, 2);
readableDatabase
= getWritableDatabase();//No permissions are required to create a database , But this line of code must be written
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(
"create table student (id integer,name varchar)");
System.out.println(
"Create");
}

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

}
}

The above is to create a database. //I’m going to talk about the important things three times

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(
"Insert data");

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 ContentValues();
contentValues1.put(
"name", "李四");
sqLiteDatabase.update(
"student", contentValues1, "id=?", new String[]{"0"}) ;//? is a placeholder, followed by String
// Element 0 of the array fills the placeholder in front?, That is, id=0, whereClause (where clause) is only a restriction condition
System.out.println("Update data");

sqLiteDatabase.delete(
"student", "id=?", new String[]{"0"});
System.out.println(
"Delete data");

deleteDatabase(
"student");
System.out.println(
"Delete database");

sqLiteDatabase.close();
}
}

public class Mysqlite extends SQLiteOpenHelper {

SQLiteDatabase readableDatabase;
public Mysqlite(Context context) {
super(context, "student", null, 2);
readableDatabase
= getWritableDatabase();//No permissions are required to create a database , But this line of code must be written
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(
"create table student (id integer,name varchar)");
System.out.println(
"Create");
}

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

}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 918 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.