如何在SQLite中存儲時間?如何僅在SQLite中存儲時間?
我已經使用時間作爲static final
的值。我被困在插入和檢索SQLite的時間。
我提到的谷歌某些網站:
SQLite沒有存儲類存儲日期 和/或時間預留。取而代之的是,內置的SQLite 的日期和時間函數能夠存儲日期和時間爲TEXT,REAL或INTEGER 值:
TEXT爲ISO8601字符串(「YYYY-MM-DD HH:MM:SS .SSS「)。 REAL Julian 天數,自格林威治中午11月中午以來的天數 24,4714 B.C.根據預測的公曆。 INTEGER 作爲Unix時間,自1970-01-01 00:00:00 UTC以來的秒數。 應用程序可以選擇以這些 格式中的任意一種格式存儲日期和時間,並使用內置日期和 時間函數在格式間自由轉換。
但我很困惑。
我得到這個錯誤。
造成的:android.database.sqlite.SQLiteException:近 「從」:語法錯誤(代碼1):在編譯:CREATE TABLE IF NOT EXISTS proname(圖像標識INTEGER,標題文本,從整型,整數);在android.database.sqlite.SQLiteConnection.nativePrepareStatem ENT(本機方法)
public static final String[] titles = new String[]{"Akilam 360","Ipadikku Idhayam",
"Palsuvai Thoranam"};
public static final String[] fromtime = new String[]{"05:00:00","07:00:00","09:00:00"};
public static final String[] totime = new String[]{"07:00:00","09:00:00","11:00:00"};
public static final Integer[] images = {R.drawable.akilam_360,
R.drawable.ipadikku_idhayam, R.drawable.palsuvai_thoranam};
ListView listView;
List<Program> rowItems;
int iImageId;
String sTitle,sFrom,sTo ;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbcon);
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR_OF_DAY);
String time = hour+":"+minutes+":"+seconds;
timer = (TextView) findViewById(R.id.timer);
timer.setText(time);
db =openOrCreateDatabase("MukilProgram", Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS proname(ImageID INTEGER,Title TEXT,From TEXT);");
rowItems = new ArrayList<Program>();
for (int i = 0; i < titles.length; i++) {
Program item = new Program(images[i], titles[i],fromtime[i],totime[i]);
rowItems.add(item);
}
db.execSQL("DELETE FROM proname;");
listView = (ListView) findViewById(listview);
final ProgramAdapter adapter = new ProgramAdapter(this,rowItems, false);
listView.setAdapter (adapter);
for (int i = 0; i < adapter.getCount(); i++) {
Program rowItem = (Program) adapter.getItem(i);
iImageId = rowItem.getImageId();
sTitle = rowItem.getTitle();
sFrom = rowItem.getFromtime();
sTo = rowItem.getTotime();
//sQuantity = rowItem.getQuantity();
db.execSQL("INSERT INTO proname VALUES("+ iImageId + ",'" + sTitle + "','"+sFrom+"','"+sTo+"');");
Toast.makeText(Databaseconnection.this, "Added to the Table..", Toast.LENGTH_SHORT).show();
}
強烈推薦 - 使用'SQLiteOpenHelper'子類。不要將所有的SQLite邏輯填入活動 –
此外,這段代碼是否工作或錯誤?因爲您只在表格中聲明瞭三列,但您已經插入了四個值。 –
代碼工作正常 – Udhaya