无法创建简单的数据库

时间:2018-05-29 11:07:52

标签: android sqlite android-sqlite

我正在制作关于伊斯兰教的最后一年项目。我想在其中使用SQLite数据库。但我无法简单地在其中创建数据库。我想在其中添加每日namaz出勤率。 第二个我如何设置列的类型到目前为止帮帮我....在此先感谢.. !!!

DataBaseHelper.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);
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码工作正常,例如使用您的代码导致: -

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中,类型并不是全部相关,因为您可以在任何类型的列中存储任何类型的值(除了 rowid 列的别名,只能是INTEGER)。

重要的是保存数据的方式。以可识别的格式/类型存储日期具有明显的优势。

e.g。按照: -

  

2.2。日期和时间数据类型SQLite没有为存储日期和/或时间而预留的存储类。相反,内置的日期和   SQLite的时间函数能够存储日期和时间   TEXT,REAL或INTEGER值:

     

TEXT as ISO8601 strings(" YYYY-MM-DD HH:MM:SS.SSS")。真如朱利安   日数,11月以来格林威治中午以来的天数   公元前24,4714根据公历格里高利历。整数   as Unix Time,自1970-01-01 00:00:00 UTC以来的秒数。   应用程序可以选择在任何这些中存储日期和时间   使用内置日期格式和格式之间自由转换   时间功能。

Datatypes In SQLite Version 3

简而言之,上面的链接必须阅读并且确实是您的问题的答案 "我如何设置列的类型到目前为止" 。它还扩展了给出的简要概述。

您可能还会发现此链接很有用Date And Time Functions

示范

以下代码是列类型的灵活性,如何存储日期以及存储访问数据的一般方法的示例。使用您的数据库和表格(参见注释和注释): -

    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是运行此时间戳的时间戳。