- package com.example.gentle.hh;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
- Created by Gentle on 2019/10/8.
*/
public class MySqlite extends SQLiteOpenHelper {
private Context context;
public MySqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
public MySqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
String s = "create table a ("
+ "_id integer primary key autoincrement,"
+ "a varchar (100),"
+ "b varchar (100),"
+ "c varchar (100) "
+ ")";
db.execSQL(s);
String sql = "create table person ("
+ "_id integer primary key autoincrement,"
+ "name varchar (100),"
+ "age varchar (100),"
+ "sex varchar (100) "
+ ")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + "person";
db.execSQL(sql);
String sql1 = "DROP TABLE IF EXISTS " + "a";
db.execSQL(sql1);
// if (newVersion > oldVersion)
onCreate(db);
}
}
package com.example.gentle.hh;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String name,name1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySqlite mySqlite = new MySqlite(this, "ff", null, 14);
SQLiteDatabase readDB = mySqlite.getReadableDatabase();
SQLiteDatabase writeDB = mySqlite.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "这是第二个表单的数据");
values.put("age", "1");
values.put("sex", "ma");
writeDB.insert("person", null, values);
ContentValues values1 = new ContentValues();
values1.put("a", "liwei");
values1.put("b", "18");
values1.put("c", "man");
writeDB.insert("a", null, values1);
/* if (falg == 1) {
Toast.makeText(this, "成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
}*/
// writeDB.close();
/* ContentValues values11 = new ContentValues();
values11.put("name", "ch");
readDB.update("person", values11, null, null);
readDB.delete("person",null,null);*/
String s = "select * from a";
Cursor cursor = readDB.rawQuery(s, null);
while (cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex("a"));
}
String ll = "select * from person";
Cursor cursor1 = readDB.rawQuery(ll, null);
while (cursor1.moveToNext()) {
name1 = cursor1.getString(cursor1.getColumnIndex("name"));
}
String a = "delete person";
Toast.makeText(this, "查询结果:" + name, Toast.LENGTH_LONG).show();
Toast.makeText(this, "查询结果:" + name1, Toast.LENGTH_LONG).show();
readDB.close();
}
}