Android一般采用什么数据库?

Android一般采用什么数据库?,第1张

Android一般采用sqlite数据库作为数据存储方案。

通常的数据库有关系型数据如:ms ql ,mysql,oracle等,非关系型nosql数据库 如mongodb,redis

android作为手机端的手机 *** 作系统,是无法直接 *** 作大型的关系型或是no sql类型的数据库的。

你的应用描述中景点等信息都可以选择存储在sqlite中。

但是图片这些信息还是建议你存储到服务器上,以文件的形式存储。

还有一种方式是在服务器中使用任何的关系型和非关系型数据库存储你的所有数据,但是你的android应用需要通过api去访问你的景点图片信息。

Android数据库 *** 作类实例

实体类:UserInfo.java

package my.db

import java.io.Serializable

import android.graphics.drawable.Drawable

public class UserInfo implements Serializable {

public static final String ID = "_id"

public static final String USERID = "userId"

public static final String TOKEN = "token"

public static final String TOKENSECRET = "tokenSecret"

public static final String USERNAME = "userName"

public static final String USERICON = "userIcon"

private String id

private String userId// 用户id

private String token

private String tokenSecret

private String userName

private Drawable userIcon

//getter and setter省略

}

SqliteHelper类:

package my.db

import android.content.Context

import android.database.sqlite.SQLiteDatabase

import android.database.sqlite.SQLiteDatabase.CursorFactory

import android.database.sqlite.SQLiteOpenHelper

import android.util.Log

public class SqliteHelper extends SQLiteOpenHelper{

//用来保存UserID、Access Token、Access Secret的表名

public static final String TB_NAME= "users"

public SqliteHelper(Context context, String name, CursorFactory factory, int version) {

super(context, name, factory, version)

}

//创建表

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL( "CREATE TABLE IF NOT EXISTS "+

TB_NAME+ "("+

UserInfo. ID+ " integer primary key,"+

UserInfo. USERID+ " varchar,"+

UserInfo. TOKEN+ " varchar,"+

UserInfo. TOKENSECRET+ " varchar,"+

UserInfo. USERNAME+ " varchar,"+

UserInfo. USERICON+ " blob"+

")"

)

Log. e("Database" ,"onCreate" )

}

//更新表

@Override

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

db.execSQL( "DROP TABLE IF EXISTS " + TB_NAME )

onCreate(db)

Log. e("Database" ,"onUpgrade" )

}

//更新列

public void updateColumn(SQLiteDatabase db, String oldColumn, String newColumn, String typeColumn){

try{

db.execSQL( "ALTER TABLE " +

TB_NAME + " CHANGE " +

oldColumn + " "+ newColumn +

" " + typeColumn

)

} catch(Exception e){

e.printStackTrace()

}

}

}

CRUD类DataHelper:

package my.db

import java.io.ByteArrayInputStream

import java.io.ByteArrayOutputStream

import java.util.ArrayList

import java.util.List

import android.content.ContentValues

import android.content.Context

import android.database.Cursor

import android.database.sqlite.SQLiteDatabase

import android.graphics.Bitmap

import android.graphics.drawable.Drawable

import android.util.Log

public class DataHelper {

// 数据库名称

private static String DB_NAME = "weibo.db"

// 数据库版本

private static int DB_VERSION = 2

private SQLiteDatabase db

private SqliteHelper dbHelper

public DataHelper(Context context) {

dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION )

db = dbHelper.getWritableDatabase()

}

public void Close() {

db.close()

dbHelper.close()

}

// 获取users表中的UserID、Access Token、Access Secret的记录

public List<UserInfo>GetUserList(Boolean isSimple) {

List<UserInfo>userList = new ArrayList<UserInfo>()

Cursor cursor = db.query(SqliteHelper. TB_NAME, null, null , null, null,

null, UserInfo. ID + " DESC")

cursor.moveToFirst()

while (!cursor.isAfterLast() &&(cursor.getString(1) != null )) {

UserInfo user = new UserInfo()

user.setId(cursor.getString(0))

user.setUserId(cursor.getString(1))

user.setToken(cursor.getString(2))

user.setTokenSecret(cursor.getString(3))

if (!isSimple) {

user.setUserName(cursor.getString(4))

ByteArrayInputStream stream = new ByteArrayInputStream(cursor.getBlob(5))

Drawable icon = Drawable.createFromStream(stream, "image")

user.setUserIcon(icon)

}

userList.add(user)

cursor.moveToNext()

}

cursor.close()

return userList

}

// 判断users表中的是否包含某个UserID的记录

public Boolean HaveUserInfo(String UserId) {

Boolean b = false

Cursor cursor = db.query(SqliteHelper. TB_NAME, null, UserInfo.USERID

+ "=?", new String[]{UserId}, null, null, null )

b = cursor.moveToFirst()

Log. e("HaveUserInfo", b.toString())

cursor.close()

return b

}

// 更新users表的记录,根据UserId更新用户昵称和用户图标

public int UpdateUserInfo(String userName, Bitmap userIcon, String UserId) {

ContentValues values = new ContentValues()

values.put(UserInfo. USERNAME, userName)

// BLOB类型

final ByteArrayOutputStream os = new ByteArrayOutputStream()

// 将Bitmap压缩成PNG编码,质量为100%存储

userIcon.compress(Bitmap.CompressFormat. PNG, 100, os)

// 构造SQLite的Content对象,这里也可以使用raw

values.put(UserInfo. USERICON, os.toByteArray())

int id = db.update(SqliteHelper. TB_NAME, values, UserInfo.USERID + "=?" , new String[]{UserId})

Log. e("UpdateUserInfo2", id + "")

return id

}

// 更新users表的记录

public int UpdateUserInfo(UserInfo user) {

ContentValues values = new ContentValues()

values.put(UserInfo. USERID, user.getUserId())

values.put(UserInfo. TOKEN, user.getToken())

values.put(UserInfo. TOKENSECRET, user.getTokenSecret())

int id = db.update(SqliteHelper. TB_NAME, values, UserInfo.USERID + "="

+ user.getUserId(), null)

Log. e("UpdateUserInfo", id + "")

return id

}

// 添加users表的记录

public Long SaveUserInfo(UserInfo user) {

ContentValues values = new ContentValues()

values.put(UserInfo. USERID, user.getUserId())

values.put(UserInfo. TOKEN, user.getToken())

values.put(UserInfo. TOKENSECRET, user.getTokenSecret())

Long uid = db.insert(SqliteHelper. TB_NAME, UserInfo.ID, values)

Log. e("SaveUserInfo", uid + "")

return uid

}

// 添加users表的记录

public Long SaveUserInfo(UserInfo user, byte[] icon) {

ContentValues values = new ContentValues()

values.put(UserInfo. USERID, user.getUserId())

values.put(UserInfo. USERNAME, user.getUserName())

values.put(UserInfo. TOKEN, user.getToken())

values.put(UserInfo. TOKENSECRET, user.getTokenSecret())

if(icon!= null){

values.put(UserInfo. USERICON, icon)

}

Long uid = db.insert(SqliteHelper. TB_NAME, UserInfo.ID, values)

Log. e("SaveUserInfo", uid + "")

return uid

}

// 删除users表的记录

public int DelUserInfo(String UserId) {

int id = db.delete(SqliteHelper. TB_NAME,

UserInfo. USERID + "=?", new String[]{UserId})

Log. e("DelUserInfo", id + "")

return id

}

public static UserInfo getUserByName(String userName,List<UserInfo>userList){

UserInfo userInfo = null

int size = userList.size()

for( int i=0i<sizei++){

if(userName.equals(userList.get(i).getUserName())){

userInfo = userList.get(i)

break

}

}

return userInfo

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9621244.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存