티스토리 뷰

JAVA

Android SQLite사용법

devlinker 2017. 7. 30. 15:24
//-----------------------------
 

public class LocationReaderDbHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "LocationReader.db";

public LocationReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public LocationReaderDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(LocationReaderContract.SQL_CREATE_ENTRIES);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(LocationReaderContract.SQL_DELETE_ENTRIES);
onCreate(db);
}

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}


}
//-------------------------------------
public final class LocationReaderContract {
private LocationReaderContract(){}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
public static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_TARGET + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_SEQUENCE + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_TYPE + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_CREATEON + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_LONGITUDE + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_LATITUDE + TEXT_TYPE + " )";

public static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;

/* Inner class that defines the table contents */
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "device";
public static final String COLUMN_NAME_TARGET = "target"; //v100,t100
public static final String COLUMN_NAME_SEQUENCE = "sequence";
public static final String COLUMN_NAME_TYPE = "type"; //Type of Location WiFi, GPSKT, Cell
public static final String COLUMN_NAME_CREATEON = "createon";
public static final String COLUMN_NAME_LONGITUDE = "longitude";
public static final String COLUMN_NAME_LATITUDE = "latitude";
}

}

 

 

 

 

 

//--------------------------------------------------------------------------
        LocationReaderDbHelper mDbHelper = new LocationReaderDbHelper(this);
        // Gets the data repository in write mode
        SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();
        values.put(FeedEntry.COLUMN_NAME_TARGET, "1062");
        values.put(FeedEntry.COLUMN_NAME_SEQUENCE, "7");
        values.put(FeedEntry.COLUMN_NAME_CREATEON, "7");
        values.put(FeedEntry.COLUMN_NAME_LONGITUDE, "37");
        values.put(FeedEntry.COLUMN_NAME_LATITUDE, "150");

// Insert the new row, returning the primary key value of the new row
        long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);

        Log.d(TAG,newRowId+"");

        db = mDbHelper.getReadableDatabase();

        String[] projection = {
                FeedEntry._ID,
                FeedEntry.COLUMN_NAME_TARGET,
                FeedEntry.COLUMN_NAME_SEQUENCE,
                FeedEntry.COLUMN_NAME_LONGITUDE,
                FeedEntry.COLUMN_NAME_LATITUDE,
                FeedEntry.COLUMN_NAME_CREATEON
        };
        String selection = FeedEntry.COLUMN_NAME_TARGET + " = ?";
        String[] selectionArgs = { "1062" };
        String sortOrder =
                FeedEntry.COLUMN_NAME_SEQUENCE + " DESC";

        Cursor c = db.query(
                FeedEntry.TABLE_NAME,                     // The table to query
                projection,                               // The columns to return
                selection,                                // The columns for the WHERE clause
                selectionArgs,                            // The values for the WHERE clause
                null,                                     // don't group the rows
                null,                                     // don't filter by row groups
                sortOrder                                 // The sort order
        );

        while(c.moveToNext()){
            Log.d(TAG,"id:"+c.getLong(c.getColumnIndexOrThrow(FeedEntry._ID))
                    +""
                    +c.getString(c.getColumnIndex(FeedEntry.COLUMN_NAME_TARGET))
                    +c.getString(c.getColumnIndex(FeedEntry.COLUMN_NAME_SEQUENCE))
                    +c.getString(c.getColumnIndex(FeedEntry.COLUMN_NAME_LATITUDE))
                    +c.getString(c.getColumnIndex(FeedEntry.COLUMN_NAME_LONGITUDE))
                    +c.getString(c.getColumnIndex(FeedEntry.COLUMN_NAME_CREATEON))
            );
        }

//--------------------------------------------------------------------------

댓글