Android数据库未在表中创建列。

huangapple go评论59阅读模式
英文:

Android database does not create columns in the table

问题

以下是您要翻译的内容:

I have a database with two tables that I want to work with. I have no problem with the first table, but when I have a database with two tables that I want, I have no problem when working with the first table, but when I insert the data in the second table, I get the "table has no clumn named" error.

This is my databaseHelper:

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public static final String TABLE_NAME = "bosors";
    public static final String _ID = "_id";
    public static final String index = "nindex";
 
    public static final String DATE = "date";
    
    static final String DB_NAME = "brs.DB";
    
    static final int DB_VERSION = 1;
    
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + index + " TEXT NOT NULL);";
    
    private static final String CREATE_TABLE2 = "create table " + "t67675656072510693" + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " TEXT NOT NULL);";
    
    @Override
    public void onCreate(SQLiteDatabase p1)
    {
        p1.execSQL(CREATE_TABLE);
        p1.execSQL(CREATE_TABLE2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase p1, int p2, int p3)
    {
        p1.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        p1.execSQL("DROP TABLE IF EXISTS " + "t67675656072510693");
        onCreate(p1);
    }

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

This is my insert:

public void insert(String data) {
    ContentValues contentValue = new ContentValues();
    contentValue.put(DatabaseHelper.index, data);
    database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
}

From here I send the data to the insert method:

if(token[i].equals(nmdName.toString())){
    dbm.insertH("t" + nmdIndex, date);
}

And when sending data to the column, I get this error:

(1) table t67675656072510693 has no column named date in "INSERT INTO t67675656072510693(date) VALUES (?)"

I tried to introduce a separate DatabaseHelper and DatabaseManager, but I encountered the same problem again.

英文:

I have a database with two tables that I want to work with. I have no problem with the first table, but when I have a database with two tables that I want, I have no problem when working with the first table, but when I insert the data in the second table, I get the "table has no clumn named" error.

This is my databaseHelper :

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public static final String TABLE_NAME = "bosors";
    public static final String _ID = "_id";
    public static final String index = "nindex";
 
  
  
	public static final String DATE = "date";
	
	
    
    static final String DB_NAME = "brs.DB";
    
    static final int DB_VERSION = 1;
    
    private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + index + " TEXT NOT NULL);";
	
	 
	
	private static final String CREATE_TABLE2 = "create table " + "t67675656072510693" + "(" + _ID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " TEXT NOT NULL);";
	
	
    @Override
    public void onCreate(SQLiteDatabase p1)
    {p1.execSQL(CREATE_TABLE);
	
	p1.execSQL(CREATE_TABLE2);
	
	// TODO: Implement this method
    }

    @Override
    public void onUpgrade(SQLiteDatabase p1, int p2, int p3)
    {p1.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		p1.execSQL("DROP TABLE IF EXISTS " + "t67675656072510693");

    onCreate(p1);
        // TODO: Implement this method
    }


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

}

This is my insert:

public void insert(String data) {
        ContentValues contentValue = new ContentValues();
        contentValue.put(DatabaseHelper.index, data);
        database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
    }

From here I send the data to the insert method:

if(token[i].equals(nmdName.toString())){
    dbm.insertH("t"+nmdIndex,date);}

And when sending data to the column, I get this error:

**** (1) table t67675656072510693 has no column named date in "INSERT INTO t67675656072510693(date) VALUES (?)"****

I tried to introduce a separate DatabaseHelper and DatabaseManager , but I encountered the same problem again.

答案1

得分: 0

  • onCreate方法中验证是否执行了create sql,并且没有记录任何错误。如果表已经被创建,可能会失败。在这种情况下,您需要删除表或执行一个更新表的SQL语句。

  • 您可以使用Android Studio或其他工具打开数据库,确认表是否实际创建。

  • 卸载应用程序或清除应用程序数据。

英文:
  • Verify if the create sql is executed in the onCreate method without any logged errors. It might fail if the table has already been created. In such cases, you need to either delete the table or execute an update table SQL statement.

  • You can use Android Studio or another tool to open the database and confirm the actual creation of the table.

  • Uninstall the application or clear the app data.

huangapple
  • 本文由 发表于 2023年6月8日 06:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定