2017-11-12 6 views
-1

これは私のデータベースファイルです パッケージorg.easysolution.easydigitalsignage.activities;なぜ私はsqliteデータベースの最後の値を取得するのですか

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; 

} 

}

これはこれは私がジェイソンからデータをフェッチし、sqliteのにそれを保存してから使用しています私のデータベース this

ある

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); 

    } 

私の主な活動のクラスですインターネットがないときにレコードを消すこと

どのように正しくsqliteでデータを格納することができますが、私は配列リストにデータを取得するときに私は最後の値を取得しています

+0

Modelクラスのリストを返すデータベースファイルのメソッドを作成します。カーソルを使用して行います。 –

答えて

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ブロックにカーソルを閉じることができます。

関連する問題