使用sqlite保存数据返回主键


  /// <summary>
/// 返回insert后的主键值
/// </summary>
/// <param name="SQLString"></param>
/// <param name="para"></param>
/// <returns></returns>
public static int ExecuteSql(string SQLString, List<SQLiteParameter> para)
{
using (SQLiteConnection connection = GetSQLiteConnection())
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
if (para!=null)
{
foreach (SQLiteParameter p in para)
{
cmd.Parameters.Add(p);
}
}
int rows = 0;
if (SQLString.IndexOf("insert") != -1)
rows = Convert.ToInt32(cmd.ExecuteScalar());
else
rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SQLiteException e)
{
connection.Close();
throw e;
}
}
}
}

注意:要在同一个连接下可以

    string sql = "insert into ims_tbl_HotelInfo(HotelId,HotelName,HotelTelephone,HotelAddress,Remark,KId,Vip,XId,YId,BId,CId,DId,EId,FId,HId) values(‘" + hotelId + "‘,‘" + hotelName + "‘,‘" + hotalTelephone + "‘,‘" + hotalAddress + "‘,‘" + hremark + "‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘,‘ ‘)";
int result = SQLiteDBHelper.ExecuteSql(sql + ";select last_insert_rowid();", null);//返回主键

使用sqlite保存数据返回主键,布布扣,bubuko.com

时间: 2024-08-08 01:12:39

使用sqlite保存数据返回主键的相关文章

jdbc:mysql和oracle插入一条数据返回主键

package org.sin.common.dao; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import or

解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题

我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用getJdbcTemplate() public int saveUser(String userName,int age,String password){ getJdbcTemplate().update(new PreparedStatementCreator() { public Prepa

MyBatis框架——mybatis插入数据返回主键(mysql、oracle)

向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是B的外键),向A表中插入数据之后,向B表中插入数据时需要用到A的主键. 比如添加一个用户,同时返回插入用户后得到的用户id: /** * 添加用户信息 * @param user * @throws Exception */ public int insertUser(User user) thro

解决mybatis3添加数据返回主键

最近项目用到插入记录后,根据生成的主键,再做其他操作,但是mybatis返回的是影响的行书,网上搜,大部分是如下形式 <insert id="add" parameterType="..." useGeneratedKeys="true" keyProperty="id"> ... </insert> 此种用法仅限于像mysql,sqlserver这样主键有自增功能的数据库,但是oracle不行. 咱也

新增数据返回主键的问题

1.表结构 CREATE TABLE `user` ( `user_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户自增id',//必须设置为自增主键 `user_name` varchar(64) NOT NULL COMMENT '用户名', `user_contactor` varchar(32) DEFAULT NULL COMMENT '联系人', `user_phone` varchar(16) DEFAULT NULL COMME

Mybatis配置插入数据返回主键ID

需要在insert方法中添加 <insert id="insertSelective" parameterType="com.midou.ott.model.MDActivity" useGeneratedKeys="true" keyProperty="id"> 加上上面红色部分,keyProperty中的id,是MDActivity对象的中的Id 使用时直接从MDActivity对象中获取到ID

Mybatis插入数据返回主键ID

<insert id="add" parameterType="com.dsa.core.base.model.ProductSync">        insert into tm_sync_product(            <if test="productId!=null">product_id,</if>            <if test="mainLimit!=null&q

SQLITE数据表主键设置Id自增方法

SQLITE数据表主键设置Id自增方法 标签: sqliteintegerinsertnulltableapi 2010-01-12 08:39 35135人阅读 评论(8) 收藏 举报  分类: SQL(11)  版权声明:本文为博主原创文章,未经博主允许不得转载. 搞定了一个困扰许久的问题,原来sqlite中的主键也是可以设置为自增的:)方法就是声明为 INTEGER PRIMARY KEY 的字段可以自动增加.    网上查到资料说,从 SQLite 的 2.3.4 版本开始,如果将一个表

Mybatis 插入数据后返回主键值

Oracle中获取刚刚插入记录的主键值: <insert id="insertSelective" parameterType="com.jxxx.p2pp.model.UUserInfo">     <selectKey resultType="java.math.BigDecimal" order="BEFORE" keyProperty="id">    SELECT U_US