如何检查然后插入sqlite?(How to check and then insert in sqlite?)
我在sqlite数据库中有一个区域表。 每次我只是在sqlite数据库上执行插入操作。 如何检查是否存在任何记录。 如果不存在,只需插入。 如果存在则更新记录。
请帮帮我。
I have a area table in sqlite database. Everytime i am just performing insert operation onto the sqlite database. How can i check if any record exists or not. If not exist simply insert. If exist then update records.
Please help me.
原文:https://stackoverflow.com/questions/20121434
更新时间:2023-10-09 16:10
最满意答案
使用
base R
我们可以用逗号分隔列,然后找到mean
。 读取数据时一定要设置stringsAsFactors=FALSE
:df$Mean <- sapply(strsplit(df$Vals, ","), function(x) mean(as.numeric(x))) df # Sample Vals Mean #1 A 1,2,3,4,5 3 #2 B 3,4,5,6,7 5 #3 C 6,7,8 7
With
base R
we can split the column by comma withstrsplit
, then find themean
. Be sure to setstringsAsFactors=FALSE
when reading in the data:df$Mean <- sapply(strsplit(df$Vals, ","), function(x) mean(as.numeric(x))) df # Sample Vals Mean #1 A 1,2,3,4,5 3 #2 B 3,4,5,6,7 5 #3 C 6,7,8 7
相关问答
更多-
在数据框中应用功能(Apply function in data frame)[2024-03-04]
您可以使用ifelse函数向量化(=在矢量中应用) if语句: myfunc = function (x) ifelse(x > 0, 1, 0) 或者,您可以使用以下更有效(但可读性更低)的方法: myfunc = function (x) as.integer(x > 0) 回到原来的功能,你的写作方式非常不像R一样。 更像R的实现看起来像这样: myfunc = function (x) if (x > 0) 1 else 0 - 无需临时变量,赋值或return语句。 ... -
数据框中的列,一些数字用双引号;(column in data frame with some numbers in double quotes; trying to change to float)[2021-11-30]
我认为'apply'会以更好的方式解决这个问题。 df ['TOT_VAL']。apply(lambda x: float (x.strip('“')))。我希望它可以提供一些帮助。 I think 'apply' will solve this in a much better way. df['TOT_VAL'].apply(lambda x : float(x.strip('"'))). I hope it can do a little help. -
使用base R我们可以用逗号分隔列,然后找到mean 。 读取数据时一定要设置stringsAsFactors=FALSE : df$Mean <- sapply(strsplit(df$Vals, ","), function(x) mean(as.numeric(x))) df # Sample Vals Mean #1 A 1,2,3,4,5 3 #2 B 3,4,5,6,7 5 #3 C 6,7,8 7 With base R ...
-
好的,我做了一个关于我的想法的简短尝试。 我可以确认以下代码段正在运行: str(d) 'data.frame': 5 obs. of 3 variables: $ a: int 1 2 3 4 5 $ b: chr "a" "a" "a" "a" ... $ c: Factor w/ 1 level "b": 1 1 1 1 1 # Get all character columns d[, sapply(d, class) == 'character'] # Or, for ...
-
在这种情况下,您可以简单地使用ifelse : sapply(1:dim(df)[1], function(i) { RowFunction(df[i, ]) }) [1] "1" "b" "3" "d" "5" "f" "7" "h" "9" "j" with(df, ifelse(y, x, z)) [1] "1" "b" "3" "d" "5" "f" "7" "h" "9" "j" 为了方便和可读性,我还使用with - 这允许您仅仅通过名称引用列,而不使用$运算符。 In this cas ...
-
R:如何从数据框中提取一些大数字,但不是其他数字(R: How to extract some large numbers, but not others, from data frame)[2022-01-27]
这是sub的一个可能的解决方案: sub(".*?([<>]?[,0-9]+)[ _]+BK.*", "\\1", vec) # [1] "18,900,000,000" "33,400,000" "900" "<250" vec是包含4个示例的矢量。 Here's a possible solution with sub: sub(".*?([<>]?[,0-9]+)[ _]+BK.*", "\\1", vec) # [1] "18,900,000,000" "33,4 ... -
尝试这个: myApp.controller("abcdctrl", ['$scope', 'orderByFilter', '$http', function ($scope, orderBy, $http) { console.log('abcdctrl'); $http.get("http://localhost:8080/api/session") .then( function successCallback(response) { $scope.data = re ...
-
用普通R : df <- data.frame(City = c('Seattle', '98125', 'Kent 98042'), Population = c(10, 20, 30), Foo = c('foo1', 'foo2', 'foo3')) df2 <- df[-grep('^\\d+$', df$City),] df2 这会产生 City Population Foo 1 Seattle ...
-
x<-"0000000000000111111000000000" y<-as.numeric(strsplit(x,split='')[[1]]) 将返回 y [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 如果你的矩阵被命名为m只是使用: m2<-apply(m,1,function(x){as.numeric(strsplit(x,split='')[[1]])}) m2<-t(m2) x<-"000000000000011 ...
-
在数据框中移动数字(Moving numbers in a data frame)[2022-01-31]
我们可以使用apply循环遍历行( MARGIN = 1 )来子集非NA元素,然后是NA元素,将它连接起来( c( ),转置( t )输出并将其分配给原始数据集 df1[] <- t(apply(df1, 1, FUN = function(x) c(x[!is.na(x)], x[is.na(x)]))) We can use apply to loop over the rows (MARGIN = 1) to subset the non-NA elements, followed by the N ...