Created
September 14, 2012 11:09
-
-
Save Matsushige/3721340 to your computer and use it in GitHub Desktop.
CSVファイルダウンロード<データベース操作クラス>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
public class DatabaseOpe { | |
public static String userInfo = ""; | |
public static void write(Context context, String _id, String type, String id, | |
String name, String code, String time) { | |
ContentValues cv = new ContentValues(); | |
cv.put("_id", _id); | |
cv.put("id", id); | |
cv.put("name", name); | |
cv.put("type", type); | |
cv.put("code", code); | |
cv.put("time", time); | |
// cv.put("level", 1); | |
SQLiteDatabase db = (new Database(context)).getReadableDatabase(); | |
db.insert("users", null, cv); | |
// String[] data = {_id}; | |
// int count = db.update("users", cv, "_id = ?", data); | |
// if(count == 0){ | |
// //該当しない場合 | |
// db.insert("users", null, cv); | |
// }// if | |
db.close(); | |
}// write | |
public static void read(Context context) { | |
try { | |
userInfo = ""; | |
SQLiteDatabase db = (new Database(context)).getReadableDatabase(); | |
Cursor c = db.query("users", null, null, null, null, null, null); | |
c.moveToFirst(); | |
for (int i = 0; i < c.getCount(); ++i) { | |
String _id = c.getString(c.getColumnIndex("_id")); | |
String type = c.getString(c.getColumnIndex("type")); | |
String id = c.getString(c.getColumnIndex("id")); | |
String name = c.getString(c.getColumnIndex("name")); | |
String code = c.getString(c.getColumnIndex("code")); | |
String time = c.getString(c.getColumnIndex("time")); | |
userInfo += _id + " " + type + " " + id + " " + name + " " + code | |
+ " " + time + "\n"; | |
c.moveToNext(); | |
}// for | |
db.close(); | |
} catch (Exception e) { | |
userInfo = "データがありません"; | |
}// catch | |
}// read | |
// public static void reset(Context context){ | |
// SQLiteDatabase db = (new Database(context)).getReadableDatabase(); | |
// ContentValues cv = new ContentValues(); | |
//// cv.put("level", 0); | |
// db.update("users", cv, null, null); | |
// db.close(); | |
// }// reset | |
public static void delete(Context context){ | |
SQLiteDatabase db = (new Database(context)).getReadableDatabase(); | |
// db.delete("users", "level = 0", null); | |
db.delete("users", null, null); | |
db.close(); | |
}// delete | |
}// DatabaseOpe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment