仍然无法创建该表。问题是什么?

发布于 2024-11-03 00:28:00 字数 2267 浏览 0 评论 0原文

这是我的数据库类

public class ProgramDbAdapter{

public static final String KEY_ROWID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
private static final String DATABASE_CREATE =
    "CREATE TABLE " + DATABASE_TABLE + " (" + "_id integer primary key autoincrement, " 
    + "ProgramTitle text, " + "ProgramDate varchar(20), "
    + "ProgramTime varchar(20), " + "ProgramChannel text)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL(DATABASE_UPGRADE);
        onCreate(db);
    }
}
public ProgramDbAdapter open() throws SQLException{
    mDbHelper = new DatabaseHelper(mcontext);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}
public long CreateData(String ProgramTitle, String ProgramDate, String ProgramTime, String ProgramChannel){
    ContentValues initialvalues = new ContentValues();
    initialvalues.put(KEY_PROGRAM_TITLE, ProgramTitle);
    initialvalues.put(KEY_PROGRAM_DATE, ProgramDate);
    initialvalues.put(KEY_PROGRAM_TIME, ProgramTime);
    initialvalues.put(KEY_PROGRAM_CHANNEL, ProgramChannel);
    return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
}

这是我的主类,

public class Program extends ExpandableListActivity{
    public void onCreate(Bundle savedInstanceState) {
        mDbHelper = new ProgramDbAdapter(this);
        mDbHelper.open();
        mDbHelper.CreateData("a","a","a","a");
    }
}

当我调用 open() 和 createdata 函数时,logcat 告诉我没有这样的表:Program。问题出在哪里?

This is my Database class

public class ProgramDbAdapter{

public static final String KEY_ROWID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
private static final String DATABASE_CREATE =
    "CREATE TABLE " + DATABASE_TABLE + " (" + "_id integer primary key autoincrement, " 
    + "ProgramTitle text, " + "ProgramDate varchar(20), "
    + "ProgramTime varchar(20), " + "ProgramChannel text)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE); 
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL(DATABASE_UPGRADE);
        onCreate(db);
    }
}
public ProgramDbAdapter open() throws SQLException{
    mDbHelper = new DatabaseHelper(mcontext);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}
public long CreateData(String ProgramTitle, String ProgramDate, String ProgramTime, String ProgramChannel){
    ContentValues initialvalues = new ContentValues();
    initialvalues.put(KEY_PROGRAM_TITLE, ProgramTitle);
    initialvalues.put(KEY_PROGRAM_DATE, ProgramDate);
    initialvalues.put(KEY_PROGRAM_TIME, ProgramTime);
    initialvalues.put(KEY_PROGRAM_CHANNEL, ProgramChannel);
    return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
}

This is my main class

public class Program extends ExpandableListActivity{
    public void onCreate(Bundle savedInstanceState) {
        mDbHelper = new ProgramDbAdapter(this);
        mDbHelper.open();
        mDbHelper.CreateData("a","a","a","a");
    }
}

when i call open() and createdata function, the logcat tell me no such table:Program. where is the problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聽兲甴掵 2024-11-10 00:28:00

sqlite 中没有 varchar 类型,请参见此处。使用文本代替。

There is no varchar type in sqlite, see here. Use text instead.

静水深流 2024-11-10 00:28:00

我不知道“DATABASE_TABLE”来自哪里。

另外,您应该使用“rawQuery()”、“query()”方法或“SQLiteStatement”将参数绑定到 SQL 语句中。

最后但并非最不重要的一点是,SQLite 不知道任何“varchar”。在此处检查所有数据类型:链接

I don't see where 'DATABASE_TABLE' is coming from.

Also, you should use the "rawQuery()", "query()"-Methods or a "SQLiteStatement" to bind parameters into your SQL-Statement.

And last but not least, SQLite doesn't know any 'varchar'. Check here for all Data types: Link

紫罗兰の梦幻 2024-11-10 00:28:00

varchar 被翻译为文本...
来自弗拉基米尔链接:

如果列的声明类型
包含任何字符串“CHAR”,
“CLOB”或“TEXT”则该列有
文本亲和力。请注意,类型
VARCHAR 包含字符串“CHAR”并且
因此分配了 TEXT 亲和力。

问题是你在第一次运行时创建了数据库,并且有不同的创建语句...

从android设置中删除应用程序数据或更改DATABASE_VERSION

varchar is translated to text ...
from vladimir link:

If the declared type of the column
contains any of the strings "CHAR",
"CLOB", or "TEXT" then that column has
TEXT affinity. Notice that the type
VARCHAR contains the string "CHAR" and
is thus assigned TEXT affinity.

problem is that u created db in first run and there was different Create statment ...

delete app data from android settings or change DATABASE_VERSION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文