这是我的数据库文件 package org.easysolution.easydigitalsignage.activities;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DATABASEVERSION4 extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "DATABASEVERSION4";
public static final String TABLE_TWOO = "TABLE_TWOO";
//TABLE TWO CONSTRAINTS
public static final String S_ID="sid";
public static final String Sr1 = "Sr1";
public static final String Sr1mediatype="Mediatype";
public static final String Sr1Path = "Path";
public static final String Sr1Align = "Alligh";
public static final String Sr1Stretch = "Strech";
public static final String Sr1Duration = "Duration";
public static final String Sr1Height = "Height";
public static final String Sr1Width = "Width";
public static final String Sr1TotalHeight = "THeiht";
public static final String Sr1TotalWidth = "TWidth";
public static final String Sr1HeightPercent = "Heightper";
public static final String Sr1WidthPercent = "Widthper";
public static final String CREATE_TABLETWO = "CREATE TABLE " + TABLE_TWOO + " ( " + S_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Sr1 + " TEXT , " + Sr1mediatype + " TEXT, " + Sr1Path + " TEXT, " + Sr1Align + " TEXT, " + Sr1Stretch + " TEXT, " + Sr1Duration + " TEXT, " + Sr1Height + " REAL, " + Sr1Width + " REAL, " + Sr1TotalHeight + " REAL, " + Sr1TotalWidth + " REAL, " + Sr1HeightPercent + " REAL, " + Sr1WidthPercent + " REAL )";
public DATABASEVERSION4(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLEONE);
sqLiteDatabase.execSQL(CREATE_TABLETWO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TWOO);
}
public String Srn() {
String Srnot = null;
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT Sr1 FROM TABLE_TWOO";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
{
do {
Srnot = cursor.getString(0);
} while (cursor.moveToNext());
}
}
return Srnot;
}
public Double Height(){
Double HeighT=null;
SQLiteDatabase database=this.getReadableDatabase();
String selectquery= "SELECT Height FROM TABLE_TWOO";
Cursor cursor=database.rawQuery(selectquery,null);
if (cursor != null && cursor.getCount() > 0)
{
cursor.moveToFirst();
{
do {
HeighT=cursor.getDouble(0);
}
while (cursor.moveToNext());
}
}
public String Widthpe() {
String WidthperT = null;
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT Widthper FROM TABLE_TWOO";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
{
do {
WidthperT = cursor.getString(0);
} while (cursor.moveToNext());
}
}
return WidthperT;
}
}
这是我的主要活动类
public void gettable_two(){
tableOprations.openDatabase();
// getdbsNo=info.Srn();
getdbmediaType=info.Mediatyp();
getdbimagePath=info.Pat();
getdballign=info.Allig();
getdbscale=info.Strec();
// getdbsduration=info.Duratio();
// getdbshght=info.Heigh();
getdheight=info.Height();
getdbswdth=info.Widt();
getdbstotalHeight=info.THeih();
getdbstotalWidth=info.TWidt();
getdbsheightPercent=info.Heightpe();
getdbswidthPercent=info.Widthpe();
Log.i("kr", String.valueOf(getdheight));
Log.i("krr", String.valueOf(getdballign));
}
public void getarraylistvalue(){
//Retrireving Array Start
getsNoArray.add(getdbsNo);
getmediaTypeArray.add(getdbmediaType);
getimagePathArray.add(getdbimagePath);
getalignArray.add(getdballign);
getscaleTypeArray.add(getdbscale);
// getdurationArray.add(gedduration);
gethghtArray.add(getdheight);
getwdthArray.add(getdwidth);
gettotalHeightArray.add(getdtotalheight);
gettotalWidthArray.add(getdtotalwidth);
getperHeightArray.add(getdheightper);
getperWidthArray.add(getidthper);
}
这是我的数据库 this
我从jason获取数据并将其存储到sqlite中,然后在没有互联网时使用它来取消记录
我可以在sqlite中正确存储数据,但是当我将数据检索到数据列表时,我只得到最后一个值
答案 0 :(得分:0)
你可以这样做。只需按照以下步骤操作我已将此用于我的应用程序。因此,只需与应用程序中的正确列和模型相关联: -
public List<NotesModel> getListOfNotes() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NOTES + " ORDER BY DATETIME(" + CREATION_DATE + ") DESC;", null);
return cursorToNotesList(res, db);
}
cursorToNotesList方法: -
private List<NotesModel> cursorToNotesList(Cursor c, SQLiteDatabase db) {
List<NotesModel> notesList = new ArrayList<>();
try {
if (c.moveToFirst()) {
do {
NotesModel notesModel = new NotesModel();
notesModel.setId(c.getInt(0));
notesModel.setTimeOfNoteCreation(c.getString(1));
notesModel.setNote(c.getString(2));
notesModel.setBackgroundColor(c.getInt(3));
notesModel.setTextColor(c.getInt(4));
notesModel.setTitle(c.getString(5));
notesModel.setPassword(c.getString(6));
notesModel.setNoteIdentifier(c.getString(7));
notesModel.setPauseText(c.getString(8));
notesModel.setUniqueNoteId(c.getString(9));
notesList.add(notesModel);
} while (c.moveToNext());
}
} catch (Exception e) {
}
return notesList;
}
此外,您可以在finally块中关闭光标。