如何在Android中的onCreate方法上创建SQLite DB?

时间:2017-04-25 11:46:59

标签: android sqlite android-sqlite

我在创建数据库时遇到问题。以下是代码的一部分:

public class DBAdapter {
    private final Context context;
    private DatabaseHelper myDBHELPER;
    private SQLiteDatabase db;
    public static int DB_VERSION = 1;
    public static final String DB_Name = "DB.db";

    //*****KONSTRUKTION DB**************************************************************************
    //*****ERSTE TABELLE: Konfiguration*************************************************************
    public static final String TABLE_Name_Konf          =       "TB_Konfig";
    public static final String KEY                      =       "id";
    public static final int KEY_Spalte                  =       0;
    public static final String SENSORSERIENNUMMER       =       "Sensornummer";
    public static final int SENSORSERIENNUMMER_SPALTE   =       1;
    public static final String[] SPALTENKonf            =       new String[]{KEY,SENSORSERIENNUMMER};

    //*****SENSOR TABELLEN**************************************************************************
    // Sting KEY kann von Konfiguration verwendet werden
    public static final String TABLE_NAME_SENS  =       "Sensor";
    public static String SENSORNUMMER;
    public static final String TIMESTAMP        =       "TimeStamp(Unix)";
    public static final String MESSWERT_Temp    =       "Temperatur";
    public static final String MESSWERT_DRUCK   =       "Druck";
    public static final String MESSWERT_FEUCHTE =       "Feuchte";
    public static final int TIMESTAMP_SPALTE    =       1;
    public static final int TEMP_SPALTE         =       2;
    public static final int DRUCK_SPALTE        =       3;
    public static final int FEUCHTE_SPALTE      =       4;
    public static final String[]  SPALTENSensor =       new String[] {KEY,TIMESTAMP,MESSWERT_Temp,MESSWERT_DRUCK,MESSWERT_FEUCHTE};




    public static final String SQL_CREATE_Konf  =       "create table "
                                                        + TABLE_Name_Konf + " ("
                                                        + KEY + " integer primary key autoincrement, " // SQL -> int
                                                        + SENSORSERIENNUMMER + " integer not null"
                                                        + ")";

    public static final String SQL_CREATE_SENS  =       "create table "
                                                        + TABLE_NAME_SENS + " ("
                                                        + KEY + " integer primary key autoincrement,"
                                                        + TIMESTAMP + " text not null,"    // SQL -> String
                                                        + MESSWERT_Temp + " real not null,"
                                                        + MESSWERT_DRUCK + " real not null,"
                                                        + MESSWERT_FEUCHTE + " real not null"
                                                        + ")";

Hier是带有onCreate的DBHelper:

 private class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DB_Name, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Toast.makeText(context, "DB erstellt", Toast.LENGTH_LONG).show();
            db.execSQL(SQL_CREATE_SENS);
        }

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

问题是,每次调用onCreate时我都会得到一个SynthaxError。我设法得到了问题的根源。我每次都可以创建数据库 + TIMESTAMP +"文字不为空,"被设置为评论。所以这必定是问题的根源,但说实话,我不明白为什么。 如果我将SQL_CREATE_Konf提供给onCreate,则可以很好地创建此数据库。 为什么这不适用于SQL_CREATE_SENS?

2 个答案:

答案 0 :(得分:1)

你的陈述中的问题如下

public static final String TIMESTAMP        =       "TimeStamp(Unix)";

是TIMESTAMP的值public static final String TIMESTAMP = "TimeStamp_Unix";

这不是有效的列名。获得一个好的描述性专栏,如

def readFile(x):
    fin = open(x, 'r')
    readline = fin.readlines()
    x,y = [], []
    for line in readline[::2]: #to skip the extra line [before editing question]
        x.append(line[0])
        y.append(line[1])

然后您可以在需要时保留时间戳的任何值。所以问题是列名,你看起来像一个生成时间戳的函数。 有关如何根据需要设置默认值的详细信息,请参阅this文档

答案 1 :(得分:0)

cout << digit1 << digit2; // but do not get anything at the compiler.

列名中不能有()括号(不引用名称)。重命名列。