2016-10-06 40 views
2

我想创建和使用一个android sqlite数据库中的临时表。
SQL语句执行时没有任何异常,但没有创建表!
运行我的应用程序下面的示例代码,我没有表:Android的SQLite不能创建TEMP表

START EDIT

public class DatabaseHelper extends SQLiteOpenHelper { 
public static final String DB_NAME = "mydb.db"; 
private static DatabaseHelper instance; 

public static synchronized DatabaseHelper getHelper(Context context){ 
    if (instance == null) 
     instance = new DatabaseHelper(context); 
    return instance; 
} 

private DatabaseHelper(Context c) { 
    super(c,DB_NAME,null,1); 
} 

@Override 
public void onConfigure(SQLiteDatabase db) { 
    super.onConfigure(db); 
    db.enableWriteAheadLogging(); 
    db.setLocale(Locale.getDefault()); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // lots of table creation 
    db.execSQL("CREATE TABLE my_table (id INTEGER PRIMARY KEY, value TEXT)"); 
    // more tables .... 
}} 
...... sample 
DatabaseHelper databaseHelper = DatabaseHelper.getHelper(this); 

编辑完

SQLiteDatabase db = databaseHelper.getWritableDatabase(); 
db.execSQL("create TEMP table my_temp_table (id integer primary key, value text)"); // no exceptions 

Cursor cursor = db.query("SQLITE_TEMP_MASTER", null, null, null, null, null, null); 

while(cur.moveToNext()) { 
    // no lines (no temporary tables) 
} 

如果我从一个创建临时表选择语句,然后查询(在相同的数据库连接)创建的表,我得到“没有这样的表...异常”!

db.execSQL("create TEMP table my_temp_table as select * from my_table"); 
Cursor cursor = db.query("temp.my_temp_table", null, null, null, null, null, null); 
^^^ no such table: temp.my_temp_table(code 1): , while compiling: SELECT * FROM temp.my_temp_table 

什么是困惑我的是......相同的SQL代码工作完全内SQLiteStudio即应用程序外部和Android设备之外。 也许我忘了启用或...我需要特定的设备权限?

+0

你能从你的databaseHelper.getWritableDatabase提供()的代码吗? – glethien

+0

当然!我用DatabaseHelper类更新了我的帖子 –

+1

你知道吗? http://www.sqlite.org/tempfiles.html#tempdb你确定,你没有关闭创建TEMP表的连接吗? – kosa

回答

0

create temp table x结果是表xtemp.x

更改代码中使用my_temp_table

+0

我改变了我的代码建议,并再次,“没有这样的表......异常”被抛出! –