美文网首页
sqlite基本操作

sqlite基本操作

作者: _弓长_大人 | 来源:发表于2018-09-25 12:43 被阅读13次

public class MainActivity extends AppCompatActivity {

EditText editText_name,editText_age;
ListView listView;

//操作数据库游标适配器
SimpleCursorAdapter cursorAdapter;
SQLiteDatabase db;
MyDBHelper dbHelper;
Cursor cursor;//游标

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText_name = findViewById(R.id.editText_name);
    editText_age = findViewById(R.id.editText_age);
    listView = findViewById(R.id.listView);

    dbHelper = new MyDBHelper(this,"person.db",null,1);
    db = dbHelper.getWritableDatabase();//在应用私有目录下建表person.db
    //cursor = db.rawQuery("select *from person",null);
    cursor = db.query("person",null,null,null,null,null,null);

    cursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"name","age"},new int[]{android.R.id.text1,android.R.id.text2},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    listView.setAdapter(cursorAdapter);
}

public void add(View v){

    //添加数据

// db.execSQL("insert into person(name,age)values(?,?)",new Object[]{editText_name.getText().toString(),editText_age.getText().toString()});
// cursor = db.query("person",null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);

    // 不用Ssql语句添加数据
   ContentValues contentValues = new ContentValues();
   contentValues.put("name",editText_name.getText().toString());
   contentValues.put("age",editText_age.getText().toString());

   db.insert("person",null,contentValues);

   //cursor = db.rawQuery("select * from person",null);
   cursor = db.query("person",null,null,null,null,null,null);
   cursorAdapter.swapCursor(cursor);
   clearEdit();
}

public void delete(View v){
    //删除数据

// db.execSQL("delete from person where name = ?",new String[]{editText_name.getText().toString()});
//
// //cursor = db.rawQuery("select * from person",null);
// cursor = db.query("person",null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);

    //不用sql语句删除数据
   db.delete("person","name = ?",new String[]{editText_name.getText().toString()});

    //cursor = db.rawQuery("select * from person",null);
    cursor = db.query("person",null,null,null,null,null,null);
    cursorAdapter.swapCursor(cursor);
    clearEdit();
}

public void update(View v){
    //更新数据

// db.execSQL("update person set age = ? where name = ?",new Object[]{editText_age.getText().toString(),editText_name.getText().toString()});
//
// // cursor = db.rawQuery("select * from person",null);
// cursor = db.query("person",null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);

    //不用sql更新数据
    ContentValues contentValues = new ContentValues();
    contentValues.put("age",editText_age.getText().toString());
    db.update("person",contentValues,"name = ?",new String[]{editText_name.getText().toString()});

    // cursor = db.rawQuery("select * from person",null);
    cursor = db.query("person",null,null,null,null,null,null);
    cursorAdapter.swapCursor(cursor);
    clearEdit();
}

public void query(View v){

    //查询

// cursor = db.query("person",null,null,null,null,null,null);
cursor = db.query("person",null,"name like ?",new String[]{"%"+editText_name.getText().toString()+"%"},null,null,null);
cursorAdapter.swapCursor(cursor);
}

public void clearEdit(){
    editText_name.setText("");
    editText_age.setText("");
    editText_name.requestFocus();
}

}

package com.someone.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper{

public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table person(_id integer primary key autoincrement,name text,age integer )");//建表
}

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

}

}

相关文章

  • sqlite基本操作

    public class MainActivity extends AppCompatActivity { // ...

  • SQLite 基本操作

    一、简介 SQLite是一种嵌入式数据库,它的数据库就是一个文件。由于SQLite本身是C写的,而且体积很小,所以...

  • Sqlite基本操作

    1.写在前面的话 最近一直在复习基础,发现自己工作这么就竟然没写过数据库的东西。然后一想好像似乎以前学的数据库几乎...

  • SQLite 基本操作

    SQLite的基本语法和MySQL的类似。 1.数据库操作 1.1 创建/打开数据库 直接使用sqlite3 Da...

  • Sqlite 使用笔记

    1. 数据库基本操作封装 sqlite 数据操作 语句类型 sqlite 数据库的简单实用- 导入sqlite3数...

  • Android-SQLite和SQLiteOpenHelper

    参考: Android 操作SQLite基本用法一、 SQLite介绍SQLite是Android内置的一个很小的...

  • android sqlite 基本操作

    sqlite是android内置的关系数据库。 网上的基础教程比较多,此处主要讲如何在项目中搭建数据库代码基础架构...

  • SQlite的基本操作

    /** 创建表*/ CREATE TABLE IF NOT EXISTS table_student ( ...

  • sqlite的基本操作(一)

    iOS开发中常用的数据存储方式主要有以下几种: 1,XML属性列表;2,Preference;3,归档;4,sql...

  • android SQLite (一)基本操作

    最近为了巩固android基础,复习了sqlite数据库这篇。好记性不如烂笔头,操起键盘就是干.... sqlit...

网友评论

      本文标题:sqlite基本操作

      本文链接:https://www.haomeiwen.com/subject/djhsdftx.html