android getint,Android cursor.getInt

博主遇到一个数据库查询的问题,涉及两个表:MAGASIN 和 LISTE。已知MAGASIN表的查询函数正常工作,但LISTE表的查询函数出现故障。问题出在尝试获取LISTE表中最大_id的查询上,具体表现为在调用cursor.getInt(0)时卡住。博主寻求解决方案。
摘要由CSDN通过智能技术生成

I got a problem with a function.

I've got a function to find the max(id) of a table and it works but with another table it doesn't work.

First, my database :

`//MAGASIN

public static final String MAGASIN_TABLE_CREATE = "CREATE TABLE "+ MAGASIN_TABLE_NAME + " ("+

MAGASIN_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " +

MAGASIN_NOM + " TEXT NOT NULL, " +

MAGASIN_ADRESSE + " TEXT);";`

`

//LISTE`

public static final String LISTE_TABLE_CREATE = "CREATE TABLE "+ LISTE_TABLE_NAME + " (" +

LISTE_KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, "+

LISTE_NAME + " TEXT NOT NULL, "+

LISTE_DATE_CREATION + " TEXT, "+

LISTE_DATE_LAST_MODIFICATION + " TEXT, "+

LISTE_IS_FINAL + " INTEGER, " +

LISTE_NUM_MAGASIN + " INTEGER, " +

"FOREIGN KEY("+ LISTE_NUM_MAGASIN +") REFERENCES MAGASIN("+ MAGASIN_KEY +"));";

After that my first function which works :

public int selectMaxNumMagasin()

{

Cursor cursor = mDb.rawQuery("select numMagasin as _id from magasin order by _id desc limit 1",null);

int max;

if (cursor == null)

{

max = 1;

return max;

}

else

{

cursor.moveToFirst();

max = cursor.getInt(0) + 1;

cursor.close();

return max;

}

}

And finally the last one which doesn't works

`

public int selectMaxNumListe()

{

Cursor cursor = mDb.rawQuery("select numListe as _id from liste order by _id desc limit 1",null);

int max;

if (cursor == null)

{

max = 1;

return max;

}

else

{

cursor.moveToFirst();

max = cursor.getInt(0);

max += 1;

cursor.close();

return max;

}

}`

I've done some tests it seems to be the

cursor.getInt(0);

that got stuck.

Hope someone will find a solution, thanks ;)

package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值