我有一个名为cv的ContentValues []数组,它进入我的SQLite数据库。为了保持简单,我的数据库有这3列(每行,日和湿度的自动增加ID)。数据库有3行。以下是添加到单行的数据示例。
#Importing all Maya commands to Python
import maya.cmds as cmds
from functools import partial
class CreateUI():
def __init__(self):
windowID = "myWindowID"
if cmds.window(windowID, exists=True):
cmds.deleteUI(windowID)
#-----------------------------------------------Layout of Interface
masterWindow = cmds.window(windowID, title="User Interface", w=500, h=800, sizeable=False, resizeToFitChildren=True)
ShapeLayout = cmds.rowColumnLayout(parent=masterWindow, numberOfColumns=1, columnOffset=[(2,"left",0)])
#-----------------------------------------------Shape Functions
#Functions for creating shapes
def mySphere(*args):
cmds.polySphere()
def myCube(*args):
cmds.polyCube()
def myCylinder(*args):
cmds.polyCylinder()
def myCone(*args):
cmds.polyCone()
def myTorus(*args):
cmds.polyTorus()
def myPlane(*args):
cmds.polyPlane()
def myDelete(*args):
cmds.select()
cmds.delete()
#Gets rid of any shapes in the scene so that the user doesn't have to every time the UI is launched or the sript is run
ShapeList = cmds.ls("mySphere*", "myCube*", "myCylinder*", "myCone*", "myTorus*", "myPlane*","pSphere*", "pCube*", "pCylinder*", "pCone*", "pTorus*", "pPlane*")
if len(ShapeList)>0:
cmds.delete(ShapeList)
#-----------------------------------------------Shape Buttons
#Buttons for creating shapes
cmds.button(label="Sphere", command=mySphere)
cmds.button(label="Cube", command=myCube)
cmds.button(label="Cylinder", command=myCylinder)
cmds.button(label="Cone", command=myCone)
cmds.button(label="Torus", command=myTorus)
cmds.button(label="Plane", command=myPlane)
cmds.button(label="Delete", command=myDelete)
#COLOUR
cmds.separator(h=20, style="none")
cmds.colorSliderGrp('blockColour',label="Colour", hsv=(120, 1, 1))
cmds.separator(h=20, style="none")
#TRANSLATE
TranslateX = cmds.intSliderGrp('TX',label="Translate X ", field=True, min=1, max=100, value=0)
TranslateY = cmds.intSliderGrp('TY',label="Translate Y ", field=True, min=1, max=100, value=0)
TranslateZ = cmds.intSliderGrp('TZ',label="Translate Z ", field=True, min=1, max=100, value=0)
cmds.separator(h=20, style="none")
#ROTATE
RotateX = cmds.intSliderGrp('RX',label="Rotate X ", field=True, min=1, max=100, value=0)
RotateY = cmds.intSliderGrp('RY',label="Rotate Y ", field=True, min=1, max=100, value=0)
RotateZ = cmds.intSliderGrp('RZ',label="Rotate Z ", field=True, min=1, max=100, value=0)
cmds.separator(h=20, style="none")
#SCALE
ScaleX = cmds.intSliderGrp('SX',label="Scale X ", field=True, min=1, max=100, value=0)
ScaleY = cmds.intSliderGrp('SY',label="Scale Y ", field=True, min=1, max=100, value=0)
ScaleZ = cmds.intSliderGrp('SZ',label="Scale Z ", field=True, min=1, max=100, value=0)
cmds.showWindow(windowID)
ui=CreateUI()
这循环3次,每次ContentValue都放入我的ContentValue数组中,然后批量插入到我的数据库中。
我有一个Job每隔五分钟从服务器获取一次新数据。所以我需要这些新数据来替换旧数据并且遇到语法问题。我直到我有ContentValues [] jsonResults与新数据,然后有点困惑。如何更新表格中的所有行?我是否需要遍历contentResolvers更新方法:
ContentValues cv = new ContentValues();
cv.put(WeatherContract.WeatherData.COLUMN_DAY_OF_WEEK, day);
cv.put(WeatherContract.WeatherData.COLUMN_HUMIDITY, humidity);
如果是这样,我在我的where和selectionArgs子句中放置什么而不是null?或者我把它保持为空?
在浏览ContentProvider之后,这是我实际插入的数据库方法:
for (int i =0; i<jsonResults.length;i++){
context.getContentResolver().update(weatherQueryUri,jsonResults[i],null,null);
}
谢谢!
答案 0 :(得分:0)
嗯,我不太确定我的解决方案是否运作良好,但你可以尝试一下。 为了用新的数据替换旧数据,我首先在ContentResolver对象上执行delete()然后执行bulkInsert()。这样你就不必循环通过jsonResults。
if (jsonResults != null && jsonResults.length != 0) {
/* Get a ContentResolver object to help delete and insert data */
ContentResolver contentResolver = context.getContentResolver();
/* Delete old data */
contentResolver.delete(weatherQueryUri,null,null);
/* Insert our new data into contentResolver */
contentResolver.bulkInsert(weatherQueryUri, jsonResults);
}