zoukankan      html  css  js  c++  java
  • Android学习之SQLite基础

    1、新建MySQLiteHelper类继承自SQLiteOpenHelper 

    public class MySQLiteHelper extends SQLiteOpenHelper {
    private Context context;
    public MySQLiteHelper(Context context, String name, CursorFactory factory,
    int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
    this.context=context;
    }

    public static final String createContact = "create table contact("
    + "id integer primary key autoincrement,"
    + "name text,phone text,email text)";

    @Override
    public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    arg0.execSQL(createContact);

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

    }

    }

    2、实现增加、修改、删除、查询

    public class MainActivity extends Activity {
    MySQLiteHelper mySQLiteHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //调用getWritableDatabase()方法,检测到当前程序中并没有contact.db数据库,于是会创建该数据库并调用MySQLiteHelper中的onCreate方法创建数据表。
    mySQLiteHelper=new MySQLiteHelper(this, "contact.db", null, 1);
    mySQLiteHelper.getWritableDatabase();
    Button btnInsert=(Button)findViewById(R.id.btnInsert);
    btnInsert.setOnClickListener(new OnClickListener() {
    //增加
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put("name", "aa");
    values.put("phone", "13989999099");
    db.insert("contact",null, values);
    values.clear();

    }
    });

    Button btnUpdate=(Button)findViewById(R.id.btnUpdate);
    btnUpdate.setOnClickListener(new OnClickListener() {
    //修改
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put("email", "itzhb@163.com");
    db.update("contact", values, "name=?",new String[]{"aa"});
    values.clear();
    }
    });

    Button btnDelete=(Button)findViewById(R.id.btnDelete);
    btnDelete.setOnClickListener(new OnClickListener() {
    //删除
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
    //db.delete("contact", "id>?",new String[]{"1"});

    //直接使用SQL操作数据库
    String sqlString="delete from contact where id=(select max(id) from contact)";
    db.execSQL(sqlString);
    }
    });

    Button btnQuery=(Button)findViewById(R.id.btnQuery);
    btnQuery.setOnClickListener(new OnClickListener() {
    //查询
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();

    //直接使用SQL查询
    Cursor cursor=db.rawQuery("select * from contact where id>?", new String[]{"1"});
    if(cursor.moveToFirst()){
    do {
    int id=cursor.getInt(cursor.getColumnIndex("id"));
    String nameString=cursor.getString(cursor.getColumnIndex("name"));
    String phoneString=cursor.getString(cursor.getColumnIndex("phone"));
    String emailString=cursor.getString(cursor.getColumnIndex("email"));
    Log.d("contact","id is "+id);
    Log.d("contact","name is "+nameString);
    Log.d("contact","phone is "+phoneString);
    Log.d("contact","email is "+emailString);
    } while (cursor.moveToNext());
    }

    }
    });
    }

    }

    另外可以通过sdkplatform-tools目录下的adb工具使用sqlite3命令管理SQLite数据库。

  • 相关阅读:
    微信跳一跳,Python辅助自动跳程序
    关于 adb devices找不到的解决方法
    vue--axios使用post方法与后台进行异步传值是报错POST http://localhost:8080/api/AddEmployeeApi 405 (METHOD NOT ALLOWED)
    vue 中引入外部css路径中使用@报错,提示找不到文件
    centos7服务器配置接口供外网调用
    排球计分程序改进
    排球计分程序(九)——总结
    排球计分程序(八)——验证编辑方法(Edit method)和编辑视图(Edit view)
    排球计分(七)—— 使用EF框架,创建Controller,生成数据库
    排球计分程序(六)——接口的设计与实现
  • 原文地址:https://www.cnblogs.com/zhouhb/p/4187527.html
Copyright © 2011-2022 走看看