GVKun编程网logo

android – Xamarin SQLite.NET Generic TableQuery

23

以上就是给各位分享android–XamarinSQLite.NETGenericTableQuery,同时本文还将给你拓展AndroidBLE4.3onDescriptorWritereturnss

以上就是给各位分享android – Xamarin SQLite.NET Generic TableQuery,同时本文还将给你拓展Android BLE 4.3 onDescriptorWrite returns status 128 on enabling characteristics notification、Android SQLite使用db.query()为JOIN而不是rawquery()、android – OrientationEventListener(Tablet vs Mobile)90度差异、android – SQLite rawQuery selectionArgs和Integers字段等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

android – Xamarin SQLite.NET Generic TableQuery

android – Xamarin SQLite.NET Generic TableQuery

我在我的Xamarin项目中使用“sqlite.NET”组件.
我有几个“模型/类”,如“文档”,“项目”.
每个模型在sqlite中都有自己的表.

要从表中获取数据,我使用以下技术:

List<Document> documents = new sqliteConnection("..DB-path..").Table<Document>.ToList();

它工作正常,但对于每个表我必须做相同的代码,只更改上面的代码中的模型类型.

现在我想写一个通用的方法,我可以做:

List<T> data = new sqliteConnection("..DB-path..").Table<T>.ToList();

但不幸的是,我收到以下错误:

‘T’ must be a non-abstract type with a public parameterless constructor in order to use it as parameter ‘T’ in the generic type or method ‘sqliteConnection.Table()’

有没有人知道为上述问题创建通用方法的方法?

提前致谢!

解决方法

你应该为你的函数添加约束.

public List<T> GetData<T> () where T: new()
{
    using(var connection = new sqliteConnection("..DB-path..")){
       return connection.Table<T>.ToList();
    }
}

不要忘记丢弃数据库连接!

Android BLE 4.3 onDescriptorWrite returns status 128 on enabling characteristics notification

Android BLE 4.3 onDescriptorWrite returns status 128 on enabling characteristics notification

question:

A question on enabling characteristics using the new android BLE 4.3:

I am not getting any notification from the BLE device though I enable notification on characteristics one by one asynchronously using a queue. 

I also write the descriptor with UUID "00002902-0000-1000-8000-00805f9b34fb" with ENABLE_NOTIFICATION_VALUE. 

I have followed the recommendation from Google sdk doc and as well suggestions from various forums. 

By the way I get status = 128 on "onDescriptorWrite". Any idea as what this status means?

I went thru google code and did not see any info on this. Even the source code does not throw any light as how this status is being set. 

Let me know if any of you have experienced this when you enabled notifications for the body media device. Also at times I get status 133 on descriptor write. I use latest Nexus 7 for my tests.

answer:

A very late answer, but this might prove valuable to anyone encountering status 128 (GATT_NO_RESOURCES) on a gatt.writedescriptor() call.

In my case status 128 showed up when trying to write a descriptor with a value of ENABLE_NOTIFICATION_VALUE for a characteristic that required a subscription for an indication via ENABLE_INDICATION_VALUE instead.

So instead of

BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(DESCRIPTOR_UUID);
descriptor.setValue(ENABLE_NOTIFICATION_VALUE);
mGatt.writeDescriptor(descriptor);

going for

BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(DESCRIPTOR_UUID);
descriptor.setValue(ENABLE_INDICATION_VALUE);
mGatt.writeDescriptor(descriptor);

Fixed the problem. I assume the other way around will produce the same error status 128.

Android SQLite使用db.query()为JOIN而不是rawquery()

Android SQLite使用db.query()为JOIN而不是rawquery()

我有tableA,tableB和tableC
表A和表B通过tableA.Id(PK)= tableB.tableAId(FK)连接,
表B和tableC通过tableB.Id(PK)= tableC.tableBId(FK)连接在一起

我想要这样做:

SELECT c.ALL from tableC c
INNER JOIN tableB b on c.tableBId = b.Id
INNER JOIN tableA a on b.tableAId = a.Id
WHERE a.Id = 108

我发现很多网站上的帖子使用db.rawquery()来实现这个查询.不过我也听说rawquery()比query()不那么安全.所以为了寻求最佳实践作为一个初学者,我的问题是:

有没有办法使用db.query()而不是db.rawquery()来实现此查询?

提前致谢.

解决方法

Is there a way to implement this query using db.query() instead of
db.rawquery()?

所以值得一提的是,rawQuery()是一个技巧.但也存在另一种方法.

query()方法用于在一个表上执行查询.但是,在sqlite中如何使用JOIN表的最佳方法是使用SQLiteQueryBuilder和setTables()方法,您可以加入.

因此,我建议您使用提及的sqliteQueryBuilder.但是,对于rawQuery()方法,您只需要分配raw语句就更复杂了.

如果不知道如何开始,请检查这个例子:

> How to use a join with SQLite

注意:

rawQuery()比query()的安全性更低,因为query()方法使用比“raw”语句更安全的预编译语句.但是,始终可以(应该)使用占位符,这显着增加了语句的安全性,作为sql注入的主要保护,语句也变得更加人性化.

android – OrientationEventListener(Tablet vs Mobile)90度差异

android – OrientationEventListener(Tablet vs Mobile)90度差异

我使用OrientationEventListener来检测方向,但我有一个问题,即平板电脑是横向的,默认情况下手机是纵向的.这意味着OrientationEventListener在平板电脑上为肖像返回值90,但在移动设备上为肖像返回0.

我正在使用的活动有相机,所以我无法在方向之间切换,因此我使用Orientation的值根据需要在屏幕上重新定位几个元素.

是否可以检测设备是否是平板电脑,以便我可以相应地调整值.即我如何计算出isTablet的价值?

if(isTablet)
            {
                orientation += -90;
                if(orientation < 0) //Check if we have gone too far back,keep the result between 0-360
                {
                    orientation += 360;
                }   
            }

解决方法

由于您已经提到无法在方向之间进行更改,因此您必须使用设备的属性来确定它是否是平板电脑.

看看this reference.

您可以使用android.os.Build.DEVICE,android.os.Build.MODEL和android.os.Build.PRODUCT来获取设备的身份,并且根据这些知识,您可以使用this参考来查找其值并确定什么是设备类型.

但是,使用此方法,每次发布新平板电脑时都必须更新软件. (我已经使用this问题作为答案的这一部分的参考).

替代是我在here找到的,引用答案:

public boolean isTablet(Context context) {
    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    return (xlarge || large);
}

LARGE and XLARGE Screen Sizes are determined by the manufacturer based
on the distance from the eye they are to be used at (thus the idea of
a tablet).

希望这可以帮助!

android – SQLite rawQuery selectionArgs和Integers字段

android – SQLite rawQuery selectionArgs和Integers字段

正如Android文档所说,rawQuery方法的selectionArgs参数被解析为字符串.

sqliteDatabase.rawQuery(String sql, String[] selectionArgs)

selectionArgs: You may include ?s in where clause in the query,
which will be replaced by the values from selectionArgs.
The values will be bound as Strings.

但是今天,我遇到了一个问题,这个问题花了我很大的一天.想象一下以下查询:

SELECT * FROM TABLE_A WHERE IFNULL(COLUMN_A, 0) >= 15

COLUMN_A是INTEGER.该表有大约10行符合该标准.在数据库编辑器上运行查询,结果始终是正确的,但是,在智能手机上,语句始终不返回任何行.

一段时间后,将查询更改为:

SELECT * FROM TABLE_A WHERE IFNULL(COLUMN_A, 0) >= '15'

并且编辑器没有返回任何行,就像Android一样.因此,将查询更改为:

SELECT * FROM TABLE_A WHERE CAST(IFNULL(COLUMN_A, 0) as INTEGER) >= '15'

解决了这个问题.另一项测试是:

SELECT * FROM TABLE_A WHERE COLUMN_A >= '15'

还,返回了正确的结果.

这似乎是一个问题,涉及Android使用IFNULL子句将参数绑定到查询(作为字符串)的方式.

那么,有人知道为什么会这样吗?如果不在查询中使用CAST,是否有任何建议可以解决这个问题?

解决方法:

将值绑定到查询的原因是为了防止SQL-injection attack.

基本上,您将查询(包括占位符)发送到您的数据库并说“我的下一个查询将是这种形式,而不是其他!”.当攻击者然后注入不同的查询字符串(例如,通过表单字段)时,数据库会说“嘿,那不是你要发送的查询!”并向你抛出一个错误.

Since commands (which can be injected into your actual query as show in the linked article) are strings, the string-datatype is the more “dangerous” one.
If a user tries to inject some code into your field which should only take numbers and you try to cast/parse the input to an integer (before putting the value into your query), you’ll get an exception right away. With a string, there is no such kind security. Therefor, they have to be escaped probably.
That might be the reason that the bind values are all interpreted as strings.

以上是假的!如果您绑定的参数是字符串或整数并不重要,它们都同样危险.此外,在代码中预先检查您的值会产生大量的样板代码,容易出错并且不灵活!

为了防止您的应用程序进行sql注入并加速多个数据库写入操作(使用具有不同值的相同查询),您应该使用“预准备语句”.在Android SDK中写入数据库的正确类是SQLiteStatement.

要创建预准备语句,请使用sqliteDatabase对象的compileStatement()-method并使用正确的bindXX()方法(从SQLiteProgram继承)绑定相应的值(将用查询中的?标记替换):

sqliteDatabase db = dbHelper.getWritableDatabase();
sqliteStatement stmt = db.compileStatement("INSERT INTO SoMetable (name, age) values (?,?)");
// Careful! The index begins at 1, not 0 !!
stmt.bindString(1, "Jon");
stmt.bindLong(2, 48L);
stmt.execute();
// Also important! Clean up after yourself.
stmt.close();

从这个较老的问题中取出的例子:How do I use prepared statements in SQlite in Android?

遗憾的是,sqliteStatement没有返回Cursor的重载,所以你不能将它用于SELECT语句.对于那些,您可以使用rawQuery(String,String []) – sqliteDatabase的方法:

int number = getNumberFromUser();
String[] arguments = new String[]{String.valueOf(number)};
db.rawQuery("SELECT * FROM TABLE_A WHERE IFNULL(COLUMN_A, 0) >= ?", arguments);

请注意,rawQuery() – 方法为参数值采用String数组.这实际上没关系,sqlite会自动转换为正确的类型.只要字符串表示与SQL查询中的预期相同,就可以了.

今天关于android – Xamarin SQLite.NET Generic TableQuery的讲解已经结束,谢谢您的阅读,如果想了解更多关于Android BLE 4.3 onDescriptorWrite returns status 128 on enabling characteristics notification、Android SQLite使用db.query()为JOIN而不是rawquery()、android – OrientationEventListener(Tablet vs Mobile)90度差异、android – SQLite rawQuery selectionArgs和Integers字段的相关知识,请在本站搜索。

本文标签: