我可以通过调用另一个 class 的方法将新列动态添加到 sqlite table 吗?
can i dynamically add new column to sqlite table by calling a method from another class?
我在数据库中有一个table,这个table的每条记录都需要存储
多个字符串,我不知道有多少个字符串,因为它是在运行时决定的。
我想在数据库 table 中动态添加图像 uri,用户
在我的应用程序中动态添加他想要的图像,所以我需要保存
它们的uri,正确的方法是什么?
我正在按照以下步骤尝试类似的操作 Insert new column into table in sqlite ?
String ColumnName=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "image1.jpg");
addNewColumn(ColumnName);
我为此使用了以下方法(无效):-
首先我要在 table 中添加新列:-
public Cursor addColumn(String name){
db=dbhelper.getWritableDatabase();
return db.rawQuery("alter table info add column " + name + " text", null);
}
然后把uri插入这个
public Boolean setUri(String columnName,String uri) {
ContentValues cv= new ContentValues();
cv.put(columnName,uri);
SQLiteDatabase db =dbhelper.getWritableDatabase();
long id=db.insert("info",null,cv);
if(id>-1)
return true;
else
return false;
}
以上做法是否正确?
我也搜索了下面的代码:-
private static final String ALTER = "ALTER TABLE user_table ADD user_street1 TEXT";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(ALTER);
}
我可以动态调用 onUpgrade() 方法并添加新列或任何其他方式来实现吗?
提前致谢:)
在 table 中有任意数量的列是非常糟糕的。您应该改用第二个 table,外键引用第一个 table.
中行的 ID
table user
_id username ...
------------------------
1 abc
2 xyz
table photoInfo
userId photoUri
-------------------------------
1 /path/to/image1.jpg
1 /path/to/image2.jpg
2 /path/to/image3.jpg
1 /path/to/image4.jpg
要显示特定用户的照片,请使用 JOIN。
我在数据库中有一个table,这个table的每条记录都需要存储 多个字符串,我不知道有多少个字符串,因为它是在运行时决定的。
我想在数据库 table 中动态添加图像 uri,用户 在我的应用程序中动态添加他想要的图像,所以我需要保存 它们的uri,正确的方法是什么?
我正在按照以下步骤尝试类似的操作 Insert new column into table in sqlite ?
String ColumnName=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + "image1.jpg");
addNewColumn(ColumnName);
我为此使用了以下方法(无效):-
首先我要在 table 中添加新列:-
public Cursor addColumn(String name){
db=dbhelper.getWritableDatabase();
return db.rawQuery("alter table info add column " + name + " text", null);
}
然后把uri插入这个
public Boolean setUri(String columnName,String uri) {
ContentValues cv= new ContentValues();
cv.put(columnName,uri);
SQLiteDatabase db =dbhelper.getWritableDatabase();
long id=db.insert("info",null,cv);
if(id>-1)
return true;
else
return false;
}
以上做法是否正确?
我也搜索了下面的代码:-
private static final String ALTER = "ALTER TABLE user_table ADD user_street1 TEXT";
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(ALTER);
}
我可以动态调用 onUpgrade() 方法并添加新列或任何其他方式来实现吗?
提前致谢:)
在 table 中有任意数量的列是非常糟糕的。您应该改用第二个 table,外键引用第一个 table.
中行的 IDtable user
_id username ...
------------------------
1 abc
2 xyz
table photoInfo
userId photoUri
-------------------------------
1 /path/to/image1.jpg
1 /path/to/image2.jpg
2 /path/to/image3.jpg
1 /path/to/image4.jpg
要显示特定用户的照片,请使用 JOIN。