1》基本操作(增删改查)
(1)创建数据表新建类MyDatabaseHelper继承SQLiteOpenHelper类,具体代码如下如所示
package com.example.sqlatetest; import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.widget.Toast; /** * Created by lenovo on 2019/1/30. */ public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_STUDENT="create table student (" +"id integer primary key autoincrement," +"studentnum integer," +"name text)"; private Context context; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version){ super( context, name, factory, version); this.context=context; } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(CREATE_STUDENT); Toast.makeText(context,"建表成功",Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { }}
通过按钮来使用这个类
package com.example.sqlatetest; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button; public class MainActivity extends AppCompatActivity { private MyDatabaseHelper myDatabaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDatabaseHelper=new MyDatabaseHelper(this,"Student.db",null,1); Button button=(Button)findViewById(R.id.b1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myDatabaseHelper.getWritableDatabase(); } }); }}
点击按钮后数据库就建立好了,安卓同样给了我们一个查看数据库的工作,首先需要在环境变量中对path变量添加palteform-tool的路径
然后打开cmd,具体操作如下如,.shema可以查看建表语句
(2)添加数据
使用按钮添加数据
button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDatabase db=myDatabaseHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("studentnum",20163489); contentValues.put("name","王*宇"); db.insert("student",null,contentValues); //清空数据 contentValues.clear(); contentValues.put("studentnum",20163490); contentValues.put("name","王*思"); db.insert("student",null,contentValues); }});
数据添加成功后在adb工具中输入select * from 表名;即可查看数据(结尾分号必须加上)
(3)修改数据
public void onClick(View view) { SQLiteDatabase db=myDatabaseHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("studentnum",20163489); contentValues.put("name","王宇"); db.update("student",contentValues,"name = ?",new String[]{"王*宇"}); }
(4)删除数据
button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDatabase db=myDatabaseHelper.getWritableDatabase(); db.delete("student","studentnum > ?",new String[]{"20163489"}); }});
(5)查询数据
查询数据时会返回一个cursor对象,数据可以逐条从中取出,query函数中最短的一个也要传入七个参数,这七个参数分别是
1,table:指定查询的表
2,columns:指定查询的列名
3,4,selection,selectionArgs:用于分页使用
5,groupby
6,having:对groupby后的数据再一次约束
7,orderby:排序方式
代码实现简单的查询:
button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SQLiteDatabase db=myDatabaseHelper.getWritableDatabase(); Cursor cursor=db.query("student",null,null,null,null,null,null); if(cursor.moveToFirst()){ do { int num=cursor.getInt(cursor.getColumnIndex("studentnum")); String name=cursor.getString(cursor.getColumnIndex("name")); Log.d(TAG, "学号: "+num); Log.d(TAG, "姓名: "+name); }while(cursor.moveToNext()); } cursor.close(); }});
SQLate到此就简单的总结完了,但是如果是使用安卓数据库的话,还是建议使用LitePal数据库,因为LitePal数据库要比SQLite好用的多得多
原文地址:https://www.cnblogs.com/837634902why/p/10336566.html
时间: 2024-10-23 06:55:25