Android编程5:数据库Sql编程
16lz
2021-01-25
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
Android中安装轻量级数据库Sqlite,现在测试数据库基本操作。
参考hosterZhu的博客,链接:http://blog.csdn.net/hosterZhu/article/details/6133927
数据库基本操作:创建表,插入,删除可以用execSQL();读取可以用rawQuery();这两个函数都可以标准SQL语句进行操作.
源代码:
package com.test.sql;import android.app.Activity;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.os.Bundle;import android.util.Log;public class test_sql extends Activity {DatabaseHelper mOpenHelper;private static final String DATABASE_NAME = "test.db";private static final int DATABASE_VERSION = 1;private static class DatabaseHelper extends SQLiteOpenHelper {DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {//新建一个用户表 //共有5项:Id Pwd Name OnLineTime Leveldb.execSQL("CREATE TABLE User_Table ('Id' INT,'Pwd' VARCHAR,'Name' VARCHAR,'OnLineTime' INT,'Level' INT)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}} /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); //清空数据 db.execSQL("DELETE FROM User_Table"); //插入数据10000 123456 "jdh" 0 0 //插入数据10001 123456 "jim" 0 0 db.execSQL("INSERT INTO User_Table VALUES (?,?,?,?,?)", new Object[]{10000,"123456","jdh",0,0}); db.execSQL("INSERT INTO User_Table VALUES (?,?,?,?,?)", new Object[]{10001,"123456","jim",0,0}); //读取数据 Cursor cursor = db.rawQuery("SELECT * FROM User_Table WHERE Name = ?",new String[]{"jdh"}); while (cursor.moveToNext()) { String str = cursor.getInt(0) + cursor.getString(1) + cursor.getString(2) + cursor.getInt(3) + cursor.getInt(4); Log.i("str:", str); } }}
更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- runOnUiThread
- android系统属性读写操作SystemProperties
- Android学习(一)(初学)SharedPreferences数据库的使用
- Android(安卓)Studio使用aidl实现进程间通信
- 在一个app中唤醒拉起另一个app应用(DeepLink拉活)
- android下webview实现H5 html标签 input file类型文件多选 multi