有没有办法检查字符串是否是有效的 UUID?
在我的情况下,我有文件列表,其中一些文件名是由 生成的 UUID UUID.randomUUID().toString(),其余的只是普通.jpg文件。
我知道通过使用UUID.fromString(filename)和捕获IllegalArgumentException抛出,我将能够检查是否filename是格式正确的 UUID。但考虑到文件列表将包含大量文件,这似乎非常昂贵。有没有一种方法可以让我在不引发异常的情况下进行检查?
我用mysql函数生成唯一和随机字符串UUID(),可以设置这个生成字符串的长度吗?我只需要8个字母。
这个插入是我用 php 插入函数做的...
先感谢您。
我希望在该特性发生更改时收到通知Micro:Bit。
\n\n我正在做的事情基本上如下:
\n\n1)检查系统是否兼容BLE
\n\n2) 启用蓝牙以防其被禁用
\n\n3) 连接到唯一一个配对设备 (Micro:Bit)
\n\n4) 当连接发生变化时激活此代码(\xc2\xbfConnected/Disconnected?)
\n\n5) 当特性更新时激活此代码\xc2\xbf?
\n\npublic class MainActivity extends Activity {\n\nBluetoothAdapter bleAdapter;\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n **(1)**\n\n if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {\n Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();\n finish();\n }\n\n **(2)**\n\n bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();\n\n if (bleAdapter == null || !bleAdapter.isEnabled()) {\n Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n startActivityForResult(enableBtIntent, 1);\n }\n\n Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();\n\n for (BluetoothDevice device : pairedDevices) {\n\n **(3)**\n\n device.connectGatt(this, true, new BluetoothGattCallback() …Run Code Online (Sandbox Code Playgroud) 这个SQL:
\n\nCREATE TABLE product_ ( \n pkey_ UUID PRIMARY KEY\n) ; \nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x80\xa6成功创建表。
\n\n但我希望新行默认为生成的 UUId,如本 AnswerRANDOM_UUID()所示。
CREATE TABLE product_ ( \n pkey_ UUID PRIMARY KEY DEFAULT RANDOM_UUID() \n) ; \nRun Code Online (Sandbox Code Playgroud)\n\n但这失败并出现错误:
\n\n\n\n\norg.h2.jdbc.JdbcSQLException: SQL 语句中的语法错误“CREATE TABLE PRODUCT_ (\n PKEY_ UUID PRIMARY KEY DEFAULT[*] RANDOM_UUID() \n ) ;”; 预期为“HASH、AUTO_INCRMENT、NOT、NULL、CHECK、REFERENCES、,、)”;SQL语句:
\n
此错误的原因和解决方法是什么?
\nIn Javascript, Node.js I can generate with https://www.npmjs.com/package/uuid package a 15 digit length "random" string. Is it possible it with Swift?
Like this: 802128100247740
const uuidv4 = require("uuid/v4");
tempUserUuid = uuidv4();
Run Code Online (Sandbox Code Playgroud) 我有以下身份验证表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class CreateAuthTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('auth', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('email', 255)->index()->unique();
$table->string('password', 96);
$table->timestamps();
});
DB::statement('ALTER TABLE auth ALTER COLUMN id SET DEFAULT uuid_generate_v4();');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('auth');
}
}
Run Code Online (Sandbox Code Playgroud)
这是模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticable;
use Illuminate\Support\Facades\Hash; …Run Code Online (Sandbox Code Playgroud) 在 React 本机项目 uuid 库中给出运行时错误
错误:不支持 crypto.getRandomValues()。请参阅 https://github.com/uuidjs/uuid#getrandomvalues-not-supported
如何使用 UUID 获取对象?我有一个对象 ( TimeCode),其属性属于xcdatamodel 文件中的id类型。UUID我想通过使用id标准中的此属性从 CoreData 获取此类型的对象NSPredicate,但出现编译错误,提示我必须将其用作ida CVarArg。但在运行时,当我实际调用该函数时,程序崩溃了,因为类型ObjectIdentifier无法转换为CVarArg.
这是完整的代码:
func fetchTimeCode(id: ObjectIdentifier) -> TimeCode? {
var timeCodes = [TimeCode]()
let request: NSFetchRequest<TimeCode> = TimeCode.fetchRequest()
request.predicate = NSPredicate(format: "id == %@", id as! CVarArg)
do {
timeCodes = try self.moc.fetch(request)
}
catch let error as NSError {
print("Error fetching for editing.")
print(error)
}
print("fetch request id: \(id)")
return timeCodes.first
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。我似乎到处寻找,却找不到任何答案。现在看来最好的解决方案是将 id 存储为字符串,但这似乎不是特别优雅。如果您无法轻松地使用 UUID …
我正在尝试使用 Spring、hibernate/JPA 和 PostgreSQL 数据库来持久保存一个简单的类。
表的列ID是一个 UUID,我想在代码中生成它,而不是在数据库中。
这应该很简单,因为 hibernate 和 postgres 对 UUID 有很好的支持。
每次我创建一个新实例并使用 写入它时save(),都会收到以下错误:
o.h.j.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement: INSERT INTO DOODAHS (fieldA, fieldB) VALUES $1, $2) ...
Run Code Online (Sandbox Code Playgroud)
此错误表明它期望在插入行时自动填充 ID 列(使用某些默认值)。
该类看起来像这样:
@lombok.Data
@lombok.AllArgsConstructor
@org.springframework.data.relational.core.mapping.Table("doodahs")
public class Doodah {
@org.springframework.data.annotation.Id
@javax.persistence.GeneratedValue(generator = "UUID")
@org.hibernate.annotations.GenericGenerator(name="UUID", strategy = "uuid2")
@javax.persistence.Column(nullable = false, unique = true)
private UUID id;
//... other fields
Run Code Online (Sandbox Code Playgroud)
我尝试过的事情:
@javax.persistence.Id使用(除了现有的 spring Id 之外)注释该字段@org.hibernate.annotations.Type(type = …在 Postgresql 更新 v15 之后,我意识到即使我有一个接受 UUID 数据类型的列,每当我尝试将 UUID 数据插入表中时,它也会抛出类似的错误。
脚本:
INSERT INTO public.testing(uuid, rating) VALUES (${uuid}, ${rating});
Run Code Online (Sandbox Code Playgroud)
错误:
运行查询错误时出错:在“45c”或附近的数字文字后尾随垃圾
Postgresql 15 发行说明:
防止数字文字具有非数字尾随字符 (Peter Eisentraut)
这个问题有什么解决办法吗?或者有另一种数据类型允许将 UUID 存储到我的表中?
uuid ×10
android ×3
postgresql ×3
php ×2
swift ×2
core-data ×1
create-table ×1
database ×1
gatt ×1
h2 ×1
hibernate ×1
ios ×1
java ×1
laravel ×1
mysql ×1
numeric ×1
primary-key ×1
query-error ×1
react-native ×1
swiftui ×1