在Android

时间:2015-06-29 11:57:28

标签: android mysql sqlite

我们可以使用今天的日期作为表名创建SQLite数据库表吗?我试过了,但它会引发错误。有没有办法用当前日期作为表名创建表?

我的代码如下:

  

我的活动是

public class MainActivity extends ActionBarActivity {

EditText et;
Button btn;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String date=DateFormat.getDateInstance().format(new Date());

    databaseHelper=new DatabaseHelper(this);
    final SQLiteDatabase db=databaseHelper.getReadableDatabase();

    et=(EditText)findViewById(R.id.editText);
    btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String str=et.getText().toString();
            if (str.equals("")){
                Toast.makeText(MainActivity.this,"NULL",Toast.LENGTH_LONG).show();
            }else {
                ContentValues cv=new ContentValues();
                try {
                    cv.put("name", str);
                    long insert=db.insert(date, null, cv);
                    if (insert!=-1){
                        Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this,"Failed",Toast.LENGTH_SHORT).show();
                    }
                }catch (SQLiteException e){
                    Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}
  

我的SQLiteOpenHelper

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String db_name="unitdb";
public static final int version=1;
Context context;

public DatabaseHelper(Context context) {
    super(context, db_name, null, version);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    String date= DateFormat.getDateInstance().format(new Date());

    db.execSQL("create table '"+date+"'(name TEXT)");
    Toast.makeText(context,"DB Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate", "Database Created");
    Log.i("Table Created", "Bike Tracker Table Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion>=newVersion)

        return;

    if (oldVersion==1){

        Log.d("New Version","Data's can be Upgraded");

    }

    Log.d("Sample Data", "onUpgrade:" + newVersion);

}

}

  

我的日志猫是

06-29 17:19:03.802    1589-1589/? E/SQLiteLog﹕ (1) near "29": syntax error
06-29 17:19:03.919      355-407/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
06-29 17:19:04.084    1589-1589/? E/SQLiteDatabase﹕ Error inserting name=dai
android.database.sqlite.SQLiteException: near "29": syntax error (code 1): ,    while compiling: INSERT INTO Jun 29, 2015(name) VALUES (?)
        at   android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at com.realtech.test.MainActivity$1.onClick(MainActivity.java:48)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

3 个答案:

答案 0 :(得分:1)

您的表名是无效的标识符。 格式化它或执行字符串操作以提出有效的标识符。 不要以数字开头。不要使用特殊字符(允许使用下划线)。 相反,标准做法,创建一个表并向其添加日期字段,使所有当前和以前的数据可供操作。

答案 1 :(得分:0)

表创建错误,您需要更改如下。

db.execSQL("create table '"+date+"'(name TEXT)");

替换为,

String tableName="your_table";// give valid name only.
db.execSQL("create table "+tableName+"(name TEXT)");

答案 2 :(得分:0)

你需要在表名中转义逗号,试试这个:

String tab_name = date.replace(","\,");

然后使用tab_name作为表的名称