此处将为大家介绍关于android–如何在SQLite中拆分逗号分隔值?的详细内容,并且为您解答有关sql按逗号拆分列为多行实例的相关问题,此外,我们还将为您介绍关于android–Retrofit:
此处将为大家介绍关于android – 如何在SQLite中拆分逗号分隔值?的详细内容,并且为您解答有关sql按逗号拆分列为多行实例的相关问题,此外,我们还将为您介绍关于android – Retrofit:如何在请求中指定逗号分隔的参数?、android – 在SQLite中保存资源路径、android – 在sqlite中获取日期差异、android – 如何在Sqlite中为TEXT列设置默认值?的有用信息。
本文目录一览:- android – 如何在SQLite中拆分逗号分隔值?(sql按逗号拆分列为多行实例)
- android – Retrofit:如何在请求中指定逗号分隔的参数?
- android – 在SQLite中保存资源路径
- android – 在sqlite中获取日期差异
- android – 如何在Sqlite中为TEXT列设置默认值?
android – 如何在SQLite中拆分逗号分隔值?(sql按逗号拆分列为多行实例)
示例:我的表中有一个Category列.
|Category | |------------------------------| |Auto,A,1234444 | |Auto,B,2345444 | |Electronincs,Computer,33443434|
我想从上面的字符串中只获取一个值.
value1: Auto value2: A value3: 1234444
我在Google上搜索了很多;我找到了一种使用Replace()和Trim()替换逗号的方法.但是,我想要一个更简单的方法.
在sql中,提供了SubString().但在sqlite中,没有这样的功能.怎么解决?
编辑:我检查了substr().但是,这个函数可以设置最大长度来获取字符串值,而我的字符串值没有固定长度.
解决方法
CREATE FUNCTION [dbo].[Split] ( @Sep char(1),@S varchar(512) ) RETURNS TABLE AS RETURN ( WITH Pieces(pn,start,stop) AS ( SELECT 1,1,CHARINDEX(@Sep,@S) UNION ALL SELECT pn + 1,stop + 1,@S,stop + 1) FROM Pieces WHERE stop > 0 ) SELECT pn,SUBSTR(@S,CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS S FROM Pieces )
android – Retrofit:如何在请求中指定逗号分隔的参数?
我的基本网址如下:
public static final String VENUES_BASE_URL = "https://api.foursquare.com/v2/venues";
我的其余网址是这样的:
"?ll=40.7,50.2&limit=50&radius=25000&v=20140909&venuePhotos=1&oauth_token=xxyyxx";
我的界面的第一个实现:
public interface Fourquare { @GET("/explore?ll={p1},{p2}&limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx") Response getVenues(@Path("p1") String param1,@Path("p2") String param2); }
然后提出这样的请求:
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(ConfigConstants.VENUES_BASE_URL) .build(); Fourquare fourquare = restAdapter.create(Fourquare.class); Response myResponse = fourquare.getVenues("50","75");
但是,上面给了我以下错误:
retrofit.RetrofitError: Fourquare.getVenues: URL query string "ll={p1},{p2}&limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx" must not have replace block.
第二个实现(在查看一些使用Query参数的SO响应之后.注意:一旦我找到了ll?参数调用,我将把令牌作为参数):
@GET("/explore&limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx") void getVenues(@Query("ll") String ll,Callback<String> cb);
通过这样的实际调用:
fourquare.getVenues("50,75",new Callback<String>() { @Override public void success(String s,Response response) { Log.d(TAG,"Successful run!"); } @Override public void failure(RetrofitError error) { Log.d(TAG,"Failed run!"); } });
通过上面的实现,fail()方法总是被调用,所以我的代码仍然有问题.有人可以就实施此调用的正确方法提出一些建议吗?我最确定问题是“ll?”参数.
更新:
转向记录后,这是我从Retrofit获得的最终网址:
https://api.foursquare.com/v2/venues/explore&limit=50&radius=25000&v=20140909&venuePhotos=1&oauth_token=xxyyxx?ll=30.26%2C-97.74
看起来Foursquare服务器不喜欢url末尾的?ll参数,它必须在../v2/venues/explore之后显式放置,因为当通过浏览器放置请求时,它工作正常.
任何解决API的限制的解决方案?
第3次实施(2014年9月17日)通过colriot建议,我能够解决我之前实施的400响应代码.我仍然遇到GSON的速度问题,所以寻找有关如何解决这个问题的建议.具体来说,与Volley相比,我的Retrofit实现需要更长的时间来显示我的结果,所以我想知道是否有更好的方法来实现回调.
Foursquare界面
public interface Fourquare { @GET("/explore?limit=50&radius=25000&v=20140909&venuePhotos=1&oauth_token=xxyyxx") void getVenues(@Query("ll") String ll,Callback<Object> cb); }
RestAdapter调用
RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(ConfigConstants.VENUES_BASE_URL) .build(); Foursquare foursquare = restAdapter.create(Foursquare.class); foursquare.getVenues("30.26,-97.74",new Callback<Object>() { @Override public void success(Object o,"Success!"); // Parse response GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); JsonParser parser = new JsonParser(); String response2 = gson.toJson(o); JsonObject data = parser.parse(response2).getAsJsonObject(); // Populate data model MetaResponse MetaResponse = gson.fromJson(data.get("Meta"),MetaResponse.class); VenuesExploreResponse myResponse = gson.fromJson(data.get("response"),VenuesExploreResponse.class); // Store results from myResponse in List } @Override public void failure(RetrofitError error) { Log.d(TAG,"Failures!"); } });
上述回调实现的当前问题是它需要比使用Volley更长(约1秒)来解析并显示结果. GsonBuilder / Gson / JsonParser块与我的Volley onResponse(String response)方法完全相同,除了那个中间的“response2”对象,所以大多数肯定这个中间/额外步骤是瓶颈.我正在寻找有关如何更好地实现Gson解析的建议.如果这可能更适合作为一个新的/单独的问题,我会这样做.
解决方法
在您的情况下,您可以像这样定义自定义类:
class LatLng { private double lat; private double lng; ... @Override public String toString() { return String.format("%.1f,%.1f",lat,lng); } }
并重构您的端点方法:
@GET("/explore?limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx") void getVenues(@Query("ll") LatLng ll,Callback<String> cb);
关于解析答案:
>永远不要在回调中创建Gson对象.它太重量了.使用您提供给RestAdapter的那个.
>为什么要混合JsonParser& GSON?对于基本相同的问题,它们是不同的工具.
>利用Retrofit的内置转换器机制;)
从单词到代码:
public class FoursquareResponse<T> { private MetaResponse Meta; private T response; // getters }
共:
@GET("/explore?limit=50&radius=25000&v=20140905&venuePhotos=1&oauth_token=xxyyxx") void getVenues(@Query("ll") LatLng ll,Callback<FoursquareResponse<VenuesExploreResponse>> cb); ... foursquare.getVenues(LatLng.valueOf(30.26,-97.74),new Callback<FoursquareResponse<VenuesExploreResponse>>() { @Override public void success(FoursquareResponse<VenuesExploreResponse> r,Response response) { MetaResponse MetaResponse = r.getMeta; VenuesExploreResponse myResponse = r.getResponse(); // Store results from myResponse in List } @Override public void failure(RetrofitError error) { Log.d(TAG,"Failures!"); } });
android – 在SQLite中保存资源路径
我的drawable文件夹中有大约300张小尺寸图片(jpg).当我运行查询时,游标返回特定字符串.现在我需要在ImageView上将其设置为ImageResource,但无论我尝试它,它似乎都不起作用.
我已经尝试将其解析为int,但似乎没有用.无论我尝试什么,logcat一直告诉我R.id.picture1不是一个有效的整数.我前段时间看到过一种可能的解决方案,但我整天都在搜索它,但似乎无法找到它.
我不想要的:将图像转换为字节数组,以便将其保存为blob.我只需要一种方法来存储和检索可绘制的引用,我可以使用它来在ImageView上设置drawable.
我不是那么有经验所以我希望有人可以帮助我.
解决方法
int resId = context.getResources().getIdentifier("picture1","drawable",context.getPackageName()); image.setimageResource(resId);;
android – 在sqlite中获取日期差异
我想获得今天和到期日之间的日期差异.这是我实现的代码.但这并没有返回正确的输出.
public String[] getDaysList(){
Cursor cursor = db.query("COUPON", null, null, null, null, null, null );
if(cursor.getCount()<1){
cursor.close();
return null;
}
String[] array = new String[cursor.getCount()];
int i=0;
while(cursor.movetoNext()){
String days = "(julianday('Now') - julianday(EXPIRED_DATE))";
array[i] = days;
i++;
}
return array;
}
这将返回(julianday(‘Now’) – julianday(EXPIRED_DATE)).请帮我在这里将日期差异作为字符串添加到数组中.
解决方法:
Now修饰符不仅返回日期,还返回时间.
要将时间戳更改为日期的开头,请使用date() function:
SELECT julianday(date('Now')) - julianday(EXPIRED_DATE) FROM ...
(如果过期列也包含时间值,则必须使用date().)
要实际执行此操作,您必须将其提供给数据库:
public String[] getDaysList() {
String days = "julianday(date('Now')) - julianday("+EXPIRED_DATE+")";
Cursor cursor = db.query("COUPON",
new String[]{ days }, // query returns one column
null, null, null, null, null);
try {
String[] array = new String[cursor.getCount()];
int i = 0;
while (cursor.movetoNext()) {
array[i++] = cursor.getString(0); // read this column
}
return array.length > 0 ? array : null;
} finally {
cursor.close();
}
}
(并且天数不是字符串;请考虑使用int [].)
android – 如何在Sqlite中为TEXT列设置默认值?
我有一张简单的桌子.我正在尝试将默认值放入TEXT列.这是表查询:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT '0', book_name TEXT NOT NULL);"
它创建表但我尝试插入数据时出现问题.我只是尝试给book_name一个书名,因为我预计book_id的列默认值为0.但它没有,它添加了空值,因此行不会插入.我也试过这个查询:
"CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT, book_id TEXT NOT NULL DEFAULT \'0\', book_name TEXT NOT NULL);"
但问题仍然存在.我已经搜索了堆栈溢出,得到了一些答案,但它们已经老了,现在还不适合我.因此,如何在sqlite中将默认值设置为TEXT列有所改变.提前致谢 :)
编辑
这是insert语句:
database.insert("book", null, cv);
这里cv是ContentValues的对象,它只包含列book_name的值.
解决方法:
您可以在创建表时为列指定默认值. (看起来好像你不能使用ALTER语句添加默认值,所以你必须重新创建你的表.)
CREATE TABLE your_table_name
(MainContactName TEXT NOT NULL DEFAULT '')
例如,
CREATE TABLE book(_id INTEGER PRIMARY KEY AUTOINCREMENT,book TEXT DEFAULT "abc");
现在看到,默认值设置为“abc”
检查sqllite documentation.
关于android – 如何在SQLite中拆分逗号分隔值?和sql按逗号拆分列为多行实例的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android – Retrofit:如何在请求中指定逗号分隔的参数?、android – 在SQLite中保存资源路径、android – 在sqlite中获取日期差异、android – 如何在Sqlite中为TEXT列设置默认值?等相关知识的信息别忘了在本站进行查找喔。
本文标签: