• 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数据库。

  • 相关阅读:
    如何区分DDR1 DDR2 DDR3内存条
    《闪电战》德军攻略
    WINDOWS SERVER 2008 R2安装指南
    【django】django学得好迷茫啊 来个学习规划吧
    【阅读】提问的智慧+有效的报告BUG
    【Python】logging模块学习笔记
    【接口测试】进度表
    【django】django深入学习笔记
    【随笔】2014工作总结
    【英语】Bingo口语笔记(47)
  • 原文地址:https://www.cnblogs.com/zhouhb/p/4187527.html
Copyright © 2020-2023  润新知