Android中关于UUID的操作

sky-mxc 总结 转载注明:https://sky-mxc.github.io

UUID

通用唯一识别码(Universally Unique Identifier)是一种软件建构的标准;
UUID的目的,是让分布式系统中的所有元素,都能有唯一的辨识信息,而不是需要通过中央控制端来做辨识信息的指定。如此以阿里,每个人都可以创建与其他人不冲突的UUID。在这种情况下,就不需要考虑数据库创建时的重复问题;

UUID是由一组32位数的16进制数字构成,UUID的标准形式包含32个16进制数字,以连字号分为五段。形式为 8-4-4-12的32个字符。

550e8400-e29b-41d4-a716-446655440000

参考链接:https://zh.wikipedia.org/wiki

UUID的生成

java中提供了 UUID类来生成UUID

1
UUID uuid = UUID.randomUUID();

UUID ,byte[],16进制字符串之间的转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* uuid转化 byte[]
* @param uuid
* @return
*/
public static byte[] toByte(UUID uuid) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
buffer.putLong(uuid.getMostSignificantBits());
buffer.putLong(uuid.getLeastSignificantBits());
return buffer.array();
}
/**
* byte[] 转换 uuid
* @param bytes
* @return
*/
public static UUID toUUID(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
long fistLong = buffer.getLong();
long secondLong = buffer.getLong();
return new UUID(fistLong, secondLong);
}
/**
* byte[] 转换为 16进制字符串啊
* @param bytes
* @return
*/
public static String bytesToHexStr(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
int v = b & 0xff;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
sb.append("0");
}
sb.append(hv);
}
return sb.toString();
}

参考链接:http://stackoverflow.com/questions/17893609/convert-uuid-to-byte-that-works-when-using-uuid-nameuuidfrombytesb

https://my.oschina.net/u/347386/blog/182717

sqlite中 guid类型数据的操作

表结构

表结构

插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void insert() {
UUID uuid = UUID.randomUUID();
String name = "mxc";
byte[] bytes = UUIDUtils.toByte(uuid);
ContentValues cv = new ContentValues();
cv.put("id",bytes);
cv.put("name",name);
SQLiteDatabase db = helper.getWritableDatabase();
db.beginTransaction();
db.insert("tb0",null,cv);
db.setTransactionSuccessful();
db.endTransaction();
}

修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private void update(){
/**
* 对于uuid的比较 ,转换为16进制字符串
*/
String hexStr = UUIDUtils.bytesToHexStr(bytes);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name","sky-mxc");
cv.put("age",23);
Log.e(TAG, "update: hexStr="+hexStr);
db.beginTransaction();
db.update("tb0",cv,"id = x'"+hexStr+"'",null);
db.setTransactionSuccessful();
db.endTransaction();
query();
}

查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void query(){
StringBuffer sb = new StringBuffer("数据如下:\n");
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("tb0", new String[]{"id", "name", "age"}, null, null, null, null, null);
while (cursor.moveToNext()){
byte[] ids = cursor.getBlob(cursor.getColumnIndex("id"));
bytes = ids;
UUID uuid = UUIDUtils.toUUID(ids);
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
sb.append("uuid="+uuid.toString());
sb.append(";\n");
String hexStr = UUIDUtils.bytesToHexStr(ids);
sb.append("hexStr="+hexStr);
sb.append(";\n");
Log.e(TAG, "query: hexStr="+hexStr);
sb.append("name="+name);
sb.append(";\n");
sb.append("age="+age);
sb.append(";\n");
sb.append("---------------------------\n");
}
cursor.close();
tvShow.setText(sb.toString());
}
坚持原创技术分享,您的支持将鼓励我继续创作!