android - 无法创建简单的数据库
问题描述
我正在做关于伊斯兰教的最后一年的项目。我想在其中使用 SQLite 数据库。但我不能简单地在其中创建数据库。我想在其中添加每日 namaz 出勤率。第二个我如何将列的类型设置为日期帮助我..提前谢谢..!!!
数据库助手.java
package com.example.shakeelmughal.assanislam;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Shakeel Mughal on 5/29/2018.
*/
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String databasename="Test.db";
public static final String Tbname = "student_table";
public static final String Col_1 = "ID";
public static final String Col_2 = "Name";
public static final String Col_3 = "Class";
public static final String Col_4 = "Marks";
public DataBaseHelper(Context context) {
super(context, databasename, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE "+Tbname+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Class TEXT, Marks INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+Tbname);
onCreate(sqLiteDatabase);
}
}
我对数据库 NamazCounterActivity,java 的主要活动
package com.example.shakeelmughal.assanislam;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class NamazCounterActivity extends AppCompatActivity {
DataBaseHelper mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_namaz_counter);
mydb = new DataBaseHelper(this);
mydb.getWritableDatabase();
//home button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
//function for going back to previous activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
解决方案
您的代码工作正常,例如使用您的代码导致:-
Database Version = 1
Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table Name = student_table Created Using = CREATE TABLE student_table(ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Class TEXT, Marks INTEGER)
Table = student_table ColumnName = ID ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
Table = student_table ColumnName = Name ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table = student_table ColumnName = Class ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
Table = student_table ColumnName = Marks ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
Table Name = sqlite_sequence Created Using = CREATE TABLE sqlite_sequence(name,seq)
这样就回答了“无法创建简单的数据库”
SQLite 中的日期没有特定的数据类型。事实上,在 SQLite 中,类型并不是那么相关,因为您可以在任何类型的列中存储任何类型的值(除了只能是 INTEGER的rowid列的别名)。
重要的是如何保存数据。以公认的格式/类型存储日期具有显着优势。
例如:-
2.2. 日期和时间数据类型 SQLite 没有为存储日期和/或时间预留存储类。相反,SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值:
TEXT 作为 ISO8601 字符串(“YYYY-MM-DD HH:MM:SS.SSS”)。REAL 作为儒略日数字,根据预测的公历,自公元前 4714 年 11 月 24 日格林威治中午以来的天数。INTEGER 作为 Unix 时间,自 1970-01-01 00:00:00 UTC 以来的秒数。应用程序可以选择以任何这些格式存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换。
SQLite 版本 3 中的数据类型
简而言之,上面的链接是必须阅读的,并且确实是您问题“我如何将列的类型设置为最新”的答案。它还扩展了给出的非常简短的概述。
您可能还会发现此链接有用的日期和时间函数
示范
以下代码是列类型的灵活性、如何存储日期以及存储访问数据的一些一般方法的示例。使用您的数据库和表格(请参阅评论和注释):-
DBHelper mydb; //<<<< Note the DatabaseHelper class has been renamed for my convenience
mydb = new DBHelper(this);
SQLiteDatabase sqldb = mydb.getWritableDatabase();
sqldb.delete(DBHelper.Tbname,null,null); //<<<< delete all rows, if any
//Prepare to insert a row
ContentValues cv = new ContentValues();
cv.put(DBHelper.Col_2,"2018-01-01 10:30:00"); //<<<< recognised date format
cv.put(DBHelper.Col_3,100.99D); //<<<< Double into a column defined as TEXT
cv.put(DBHelper.Col_4, "Sorry you scored nothing"); //<<<< Text into an INTEGER
sqldb.insert(
DBHelper.Tbname,
null,
cv
);
cv.clear();
// Prepare to insert a 2nd row
cv.put(DBHelper.Col_2,"Fred");
cv.put(DBHelper.Col_3,System.currentTimeMillis()); //<<<<< can be used stores long as INTEGER
sqldb.insert(DBHelper.Tbname,null,cv);
// get all the rows from the table
Cursor csr = sqldb.query(DBHelper.Tbname,
null,
null,
null,
null,
null,
null
);
while (csr.moveToNext()) {
Log.d("DATA",
"ROW " + csr.getPosition() +
" has a value of " +
String.valueOf(csr.getLong(csr.getColumnIndex(DBHelper.Col_1))) +
" in the " + DBHelper.Col_1 +
" column, a value of " +
csr.getString(csr.getColumnIndex(DBHelper.Col_2)) +
" in the " +
DBHelper.Col_2 +
" column, a value of " +
csr.getString(csr.getColumnIndex(DBHelper.Col_3)) +
" in the " +
DBHelper.Col_3 +
" column, and a value of " +
csr.getString(csr.getColumnIndex(DBHelper.Col_4)) +
" in the " +
DBHelper.Col_4 +
" column."
);
}
csr.close(); //<<<< Should always close a Cursor when done with it
以上结果将以下内容输出到日志:-
ROW 0 has a value of 5 in the ID column, a value of 2018-01-01 10:30:00 in the Name column, a value of 100.99 in the Class column, and a value of Sorry you scored nothing in the Marks column.
ROW 1 has a value of 6 in the ID column, a value of Fred in the Name column, a value of 1527600028305 in the Class column, and a value of null in the Marks column.
- 由于应用程序的几次运行,id 高于预期。
- 1527600028305 是运行时的时间戳。
推荐阅读
- html - 如何制作自动选择 URL 的 whatsapp 共享按钮
- python - Python Pandas 重新采样不会重新采样
- c# - 找到移动的文件?
- powershell - Import-PfxCertificate 不在根存储中插入我的 CA
- datastage - 在 DataStage 中的文件中捕获拒绝记录
- java - NoClassDefFoundError: org/w3c/dom/ls/DocumentLS with com.sun.msv.generator.Driver
- powershell - Powershell条件发送邮件
- sql - 用于创建添加记录时的时间戳的列
- amazon-s3 - 使用 Terraform 工作空间时未创建 S3 状态文件
- powershell - 启动新的PowerShell实例并删除文件夹的问题