oracle S3工具类 OCI S3工具类 解决S3生成预签名url前端跨域问题 S3 objectstorage api工具栏

objectstorage API 官方文档: https://docs.oracle.com/en-us/iaas/api/#/en/objectstorage/20160918/

介绍

使用对象存储和存档存储 API 来管理存储桶、对象和相关资源。有关详细信息,请参阅对象存储概述和存档存储概述。
Oracle 云基础设施对象存储服务是一个互联网规模的高性能存储平台,可提供可靠且经济高效的数据持久性。 对象存储服务可以存储无限量的任何内容类型的非结构化数据,包括分析数据和丰富的内容,如图像和视频。

注意事项

生成URL前端跨域问题
1. Amazon(亚马逊) 解决方案:

官方解决跨域文档地址: https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/userguide/enabling-cors-examples.html
问题解决及工具类: https://blog.csdn.net/ayunnuo/article/details/127211608

2. OCI(Oracle) 解决方案: 使用OCI原生服务: API Amazon Simple Storage

官方文档地址: https://docs.amazonaws.cn/AmazonS3/latest/userguide/upload-objects.html

POM依赖


 <!-- 依赖声明 -->
 <dependencyManagement>
     <dependencies>
     
         <!-- oci -->
         <dependency>
             <groupId>com.oracle.oci.sdk</groupId>
             <artifactId>oci-java-sdk-bom</artifactId>
             <version>2.44.0</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         
     </dependencies>
 </dependencyManagement>

    <!-- https://mvnrepository.com/artifact/com.oracle.oci.sdk/oci-java-sdk-objectstorage -->
     <dependency>
         <groupId>com.oracle.oci.sdk</groupId>
         <artifactId>oci-java-sdk-objectstorage</artifactId>
     </dependency>
 
     <!-- lombok  -->
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
     </dependency>

     <!--    hutool工具类    -->
     <dependency>
         <groupId>cn.hutool</groupId>
         <artifactId>hutool-all</artifactId>
         <version>5.8.3</version>
     </dependency>
     

DTO

1. S3BaseConfig

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * s3基础配置Bean
 *
 * @author yunnuo <a href="2552846359@qq.com">E-Mail: 2552846359@qq.com</a>
 * @since 1.0.0
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "S3基础配置 Bean")
public class S3BaseConfig {

    /**
     * s3秘密访问密钥
     */
    @ApiModelProperty(value = "s3秘密访问密钥")
    private String s3SecretAccessKey;

    /**
     * s3访问密钥id
     */
    @ApiModelProperty(value = "s3访问密钥id")
    private String s3AccessKeyId;

    /**
     * s3 bucket
     */
    @ApiModelProperty(value = "s3 bucket")
    private String s3Bucket;

    /**
     * 地区
     */
    @ApiModelProperty(value = "地区")
    private String regions;

    /**
     * 类型 0=aws环境 1=oracle环境, default= 0
     */
    @ApiModelProperty(value = "类型 0=aws环境 1=oracle环境, default=0")
    private Integer type = 0;

    /**
     * aws环境可空, oci环境使用
     */
    @ApiModelProperty(value = "namespace")
    private String namespace;

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}


2. S3CommonConfig

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

/**
 * s3 配置中心通用配置
 *
 * @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>
 * @see S3CommonConfig : {@code 参考配置中心key: bt-user.s3.images.config}
 * @since 1.0.0
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class S3CommonConfig extends S3BaseConfig {

    /**
     * 标题
     */
    @ApiModelProperty(value = "标题")
    private String title;

    /**
     * 描述
     */
    @ApiModelProperty(value = "描述")
    private String desc;

    /**
     * cdn前缀
     */
    @ApiModelProperty(value = "cdn前缀")
    private String cdnPrefix;


    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }

}

OCI Object Storage Util 工具类

注意: 此工具类生产的预前面上传url 支持跨域操作


import com.kabak.rongsheng.domain.dto.s3.S3BaseConfig;
import com.kabak.rongsheng.domain.dto.s3.S3CommonConfig;
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.model.CreatePreauthenticatedRequestDetails;
import com.oracle.bmc.objectstorage.model.PreauthenticatedRequest;
import com.oracle.bmc.objectstorage.model.RenameObjectDetails;
import com.oracle.bmc.objectstorage.requests.*;
import com.oracle.bmc.objectstorage.responses.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * oci Object Storage 版本工具类
 *
 * @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>
 * @since 1.0.0
 */
public class OciS3Utils {

    public static final String PRE_URL = "https://objectstorage.<region_ID>.oraclecloud.com<access-uri><objectName>";

    /**
     * 复制对象
     *
     * @param client            客户端
     * @param copyObjectRequest 复制对象请求
     * @return {@link CopyObjectResponse}
     */
    public static CopyObjectResponse copyObject(ObjectStorageClient client, CopyObjectRequest copyObjectRequest) {
        return client.copyObject(copyObjectRequest);
    }


    /**
     * 重命名对象
     *
     * @param client     客户端
     * @param namespace  名称空间
     * @param bucketName bucket名称
     * @param sourceName 原名称
     * @param newName    新名字
     * @return {@link RenameObjectResponse}
     */
    public static RenameObjectResponse renameObject(ObjectStorageClient client, String namespace, String bucketName, String sourceName, String newName) {
        RenameObjectDetails renameObjectDetails = RenameObjectDetails.builder()
                .sourceName(sourceName)
                .newName(newName).build();
        RenameObjectRequest renameObjectRequest = RenameObjectRequest.builder()
                .namespaceName(namespace)
                .bucketName(bucketName)
                .renameObjectDetails(renameObjectDetails).build();
        return client.renameObject(renameObjectRequest);
    }

    /**
     * 重命名对象
     *
     * @param client              客户端
     * @param renameObjectRequest 重命名对象请求
     * @return {@link RenameObjectResponse}
     */
    public static RenameObjectResponse renameObject(ObjectStorageClient client, RenameObjectRequest renameObjectRequest) {
        return client.renameObject(renameObjectRequest);
    }

    /**
     * 获取对象
     *
     * @param client     客户端
     * @param namespace  名称空间
     * @param bucketName bucket名称
     * @param key        key
     * @return {@link GetObjectResponse}
     */
    public static GetObjectResponse getObject(ObjectStorageClient client, String namespace, String bucketName, String key) {
        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                .namespaceName(namespace)
                .bucketName(bucketName)
                .objectName(key).build();
        return client.getObject(getObjectRequest);
    }

    /**
     * 获取对象
     *
     * @param client        客户端
     * @param objectRequest 对象请求
     * @return {@link GetObjectResponse}
     */
    public static GetObjectResponse getObject(ObjectStorageClient client, GetObjectRequest objectRequest) {
        return client.getObject(objectRequest);
    }

    /**
     * 删除对象
     *
     * @param client     客户端
     * @param namespace  名称空间
     * @param bucketName bucket名称
     * @param key        key
     * @return {@link DeleteObjectResponse}
     */
    public static DeleteObjectResponse deleteObject(ObjectStorageClient client, String namespace, String bucketName, String key) {
        DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
                .namespaceName(namespace)
                .bucketName(bucketName)
                .objectName(key).build();
        return client.deleteObject(deleteObjectRequest);
    }

    /**
     * 删除对象
     *
     * @param client              客户端
     * @param deleteObjectRequest 删除对象请求
     * @return {@link DeleteObjectResponse}
     */
    public static DeleteObjectResponse deleteObject(ObjectStorageClient client, DeleteObjectRequest deleteObjectRequest) {
        return client.deleteObject(deleteObjectRequest);
    }

    /**
     * 获取上传单个文件 默认:bundle/date/key 预授权url
     * <p color='red'> 注意: 如果需要上传多个文件到同一个目录下, 请使用{@code getPreAuthAnyObjectPathReadWriteURL} </p>
     *
     * @param client      客户端
     * @param config      配置
     * @param name        名字(主要是在Console里面起显示作用, 不允许重复)
     * @param timeExpires 过期时间
     * @param key         key
     * @return {@link String}
     */
    public static String getPreAuthObjDefaultBundleDateURL(ObjectStorageClient client, S3CommonConfig config, String name, Date timeExpires, String... key) {
        String filePath = S3CommonUtils.getBundleCurrentDateKey(config.getS3Bucket(), S3CommonUtils.formatFilePath(key));
        CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
        return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
    }

    /**
     * 获取上传单个文件 预授权url
     * <p color='red'> 注意: 如果需要上传多个文件到同一个目录下, 请使用{@code getPreAuthAnyObjectPathReadWriteURL} </p>
     *
     * @param client      客户端
     * @param config      配置
     * @param name        名字(主要是在Console里面起显示作用, 不允许重复)
     * @param timeExpires 过期时间
     * @param key         key
     * @return {@link String}
     */
    public static String getPreAuthAnyObjectReadWriteURL(ObjectStorageClient client, S3BaseConfig config, String name, Date timeExpires, String... key) {
        String filePath = S3CommonUtils.formatFilePath(key);
        CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
        return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
    }

    /**
     * 获取预身份验证对象路径 [权限: 读写]  默认:bundle/date 预授权url
     *
     * @param client      客户端
     * @param config      配置
     * @param name        名字(主要是在Console里面起显示作用, 不允许重复)
     * @param timeExpires 过期时间
     * @return {@link String}
     */
    public static String getPreAuthObjPathDefaultBundleDateURL(ObjectStorageClient client, S3BaseConfig config, String name, String bundle, Date timeExpires) {
        String filePath = S3CommonUtils.getBundleCurrentDateKey(bundle, "");
        CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, filePath, timeExpires);
        return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
    }

    /**
     * 获取预身份验证对象路径[权限: 读写] URL
     * <p color='red'>解决性能问题: prefixKey可以传一个前缀路径, 然后通过生成的url地址拼接对象名称进行上传,防止上传多个文件到同一个文件夹下申请多次url</p>
     *
     * @param client      客户端
     * @param config      配置
     * @param name        名字(主要是在Console里面起显示作用, 不允许重复)
     * @param prefixKey   前缀路径
     * @param timeExpires 过期时间
     * @return {@link String}
     */
    public static String getPreAuthAnyObjectPathReadWriteURL(ObjectStorageClient client, S3BaseConfig config, String name, String prefixKey, Date timeExpires) {
        CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, prefixKey, timeExpires);
        return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
    }


    /**
     * 获取预身份验证对象路径[权限: 读写] URL
     * <p color='red'>解决性能问题: prefixKey可以传一个前缀路径, 然后通过生成的url地址拼接对象名称进行上传,防止上传多个文件到同一个文件夹下申请多次url</p>
     *
     * @param client      客户端
     * @param config      配置
     * @param name        名字(主要是在Console里面起显示作用, 不允许重复)
     * @param prefixKey   前缀key
     * @param timeExpires 过期时间
     * @return {@link String}
     */
    public static String getPreAuthAnyObjectPathReadWriteURL(ObjectStorageClient client, S3CommonConfig config, String name, String prefixKey, Date timeExpires) {
        CreatePreauthenticatedRequestResponse response = getPreAuthAnyObjectReadWriteResponse(client, config.getS3Bucket(), config.getNamespace(), name, prefixKey, timeExpires);
        return getPreAuthUrl(config.getRegions(), response.getPreauthenticatedRequest());
    }


    /**
     * 获取预身份验证对象[权限: 读写]响应
     *
     * @param client        客户端
     * @param bucketName    bucket名称
     * @param namespaceName 名称空间名字
     * @param name          名字(主要是在Console里面起显示作用, 不允许重复)
     * @param key           key
     * @param timeExpires   过期时间
     * @return {@link CreatePreauthenticatedRequestResponse}
     */
    public static CreatePreauthenticatedRequestResponse getPreAuthAnyObjectReadWriteResponse(ObjectStorageClient client, String bucketName, String namespaceName, String name, String key, Date timeExpires) {
        CreatePreauthenticatedRequestDetails createPreauthenticatedRequestDetails = CreatePreauthenticatedRequestDetails.builder()
                .name(name)
                .bucketListingAction(PreauthenticatedRequest.BucketListingAction.ListObjects)
                .objectName(key)
                .accessType(CreatePreauthenticatedRequestDetails.AccessType.AnyObjectReadWrite)
                .timeExpires(timeExpires).build();

        CreatePreauthenticatedRequestRequest createPreauthenticatedRequestRequest = CreatePreauthenticatedRequestRequest.builder()
                .namespaceName(namespaceName)
                .bucketName(bucketName)
                .createPreauthenticatedRequestDetails(createPreauthenticatedRequestDetails).build();

        return client.createPreauthenticatedRequest(createPreauthenticatedRequestRequest);
    }

    /**
     * 上传对象到默认包/日期/key下
     *
     * @param config        配置
     * @param client        客户端
     * @param bundle        包
     * @param contentLength 内容长度
     * @param contentType   内容类型
     * @param key           key
     * @param objectContent 对象内容
     * @return {@link PutObjectResponse}
     */
    public static PutObjectResponse putObjectDefaultBundleDate(S3BaseConfig config, ObjectStorageClient client, String bundle, InputStream objectContent, Long contentLength, String contentType, String... key) {
        final String bundleCurrentDateKey = S3CommonUtils.getBundleCurrentDateKey(bundle, S3CommonUtils.formatFilePath(key));
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(config.getNamespace())
                .bucketName(config.getS3Bucket())
                .objectName(bundleCurrentDateKey)
                .putObjectBody(objectContent)
                .contentLength(contentLength)
                .contentType(contentType).build();
        return getPutObjectRequest(client, putObjectRequest);
    }

    /**
     * 上传对象到默认包/日期/key下
     *
     * @param config        配置
     * @param client        客户端
     * @param bundle        包
     * @param contentLength 内容长度
     * @param contentType   内容类型
     * @param key           key
     * @param objectContent 对象内容
     * @return {@link PutObjectResponse}
     */
    public static PutObjectResponse putObjectDefaultBundleDate(S3CommonConfig config, ObjectStorageClient client, String bundle, InputStream objectContent, Long contentLength, String contentType, String... key) {
        final String bundleCurrentDateKey = S3CommonUtils.getBundleCurrentDateKey(bundle, S3CommonUtils.formatFilePath(key));
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(config.getNamespace())
                .bucketName(config.getS3Bucket())
                .objectName(bundleCurrentDateKey)
                .putObjectBody(objectContent)
                .contentLength(contentLength)
                .contentType(contentType).build();
        return getPutObjectRequest(client, putObjectRequest);
    }

    /**
     * 上传对象
     *
     * @param config        配置
     * @param client        客户端
     * @param contentLength 内容长度
     * @param contentType   内容类型
     * @param key           key
     * @param objectContent 对象内容
     * @return {@link PutObjectResponse}
     */
    public static PutObjectResponse putObject(S3BaseConfig config, ObjectStorageClient client, InputStream objectContent, Long contentLength, String contentType, String... key) {
        String filePath = S3CommonUtils.formatFilePath(key);
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(config.getNamespace())
                .bucketName(config.getS3Bucket())
                .objectName(filePath)
                .putObjectBody(objectContent)
                .contentLength(contentLength)
                .contentType(contentType).build();
        return getPutObjectRequest(client, putObjectRequest);
    }


    /**
     * 上传对象
     *
     * @param config        配置
     * @param client        客户端
     * @param contentLength 内容长度
     * @param contentType   内容类型
     * @param key           key
     * @param objectContent 对象内容
     * @return {@link PutObjectResponse}
     */
    public static PutObjectResponse putObject(S3CommonConfig config, ObjectStorageClient client, InputStream objectContent, Long contentLength, String contentType, String... key) {
        String filePath = S3CommonUtils.formatFilePath(key);
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(config.getNamespace())
                .bucketName(config.getS3Bucket())
                .objectName(filePath)
                .putObjectBody(objectContent)
                .contentLength(contentLength)
                .contentType(contentType).build();
        return getPutObjectRequest(client, putObjectRequest);
    }

    /**
     * 获取 上传对象请求
     *
     * @param client           客户端
     * @param putObjectRequest 把对象请求
     * @return {@link PutObjectResponse}
     */
    public static PutObjectResponse getPutObjectRequest(ObjectStorageClient client, PutObjectRequest putObjectRequest) {
        return client.putObject(putObjectRequest);
    }


    /**
     * 获取默认client
     *
     * @return {@link ObjectStorageClient}
     * @throws IOException ioexception
     */
    public static ObjectStorageClient getDefaultClient() throws IOException {
        final ConfigFileAuthenticationDetailsProvider provider = getProvider(ConfigFileReader.parseDefault());
        return getClient(provider);
    }

    /**
     * 获取client
     *
     * @param configFilePath 配置文件路径
     * @return {@link ObjectStorageClient}
     * @throws IOException ioexception
     */
    public static ObjectStorageClient getClient(String configFilePath) throws IOException {
        final ConfigFileAuthenticationDetailsProvider provider = getProvider(configFilePath);
        return getClient(provider);
    }

    /**
     * 获取client
     *
     * @param configFile 配置文件
     * @return {@link ObjectStorageClient}
     */
    public static ObjectStorageClient getClient(ConfigFileReader.ConfigFile configFile) {
        final ConfigFileAuthenticationDetailsProvider provider = getProvider(configFile);
        return getClient(provider);
    }

    /**
     * 获取client
     *
     * @param provider 提供者
     * @return {@link ObjectStorageClient}
     */
    public static ObjectStorageClient getClient(ConfigFileAuthenticationDetailsProvider provider) {
        return new ObjectStorageClient(provider);
    }


    /**
     * 获取提供者
     *
     * @param configFilePath 配置文件路径
     * @return {@link ConfigFileAuthenticationDetailsProvider}
     * @throws IOException ioexception
     */
    public static ConfigFileAuthenticationDetailsProvider getProvider(String configFilePath) throws IOException {
        final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parse(configFilePath);
        return getProvider(configFile);

    }

    /**
     * 获取提供者
     *
     * @param configFile 配置文件
     * @return {@link ConfigFileAuthenticationDetailsProvider}
     */
    public static ConfigFileAuthenticationDetailsProvider getProvider(ConfigFileReader.ConfigFile configFile) {
        return new ConfigFileAuthenticationDetailsProvider(configFile);

    }


    /**
     * 获取预身份验证url
     *
     * @param region                  地区
     * @param preauthenticatedRequest preauthenticated请求
     * @return {@link String}
     */
    public static String getPreAuthUrl(String region, PreauthenticatedRequest preauthenticatedRequest) {
        return getPreAuthUrl(region, preauthenticatedRequest.getAccessUri(), preauthenticatedRequest.getObjectName());
    }

    /**
     * 获取预身份验证url
     *
     * @param accessUri  uri访问
     * @param objectName 对象名称
     * @param region     地区
     * @return {@link String}
     */
    public static String getPreAuthUrl(String region, String accessUri, String objectName) {
        return PRE_URL.replace("<region_ID>", region)
                .replace("<access-uri>", accessUri)
                .replace("<objectName>", objectName);
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的封装OCI的C++类,实现对Oracle数据库的增删改查操作。 ```c++ #include <iostream> #include <string> #include <oci.h> class OracleDB { public: OracleDB() : envhp(nullptr), errhp(nullptr), srvhp(nullptr), svchp(nullptr), stmthp(nullptr) {} ~OracleDB() { if (envhp) OCIHandleFree(envhp, OCI_HTYPE_ENV); if (errhp) OCIHandleFree(errhp, OCI_HTYPE_ERROR); if (srvhp) OCIHandleFree(srvhp, OCI_HTYPE_SERVER); if (svchp) OCIHandleFree(svchp, OCI_HTYPE_SVCCTX); if (stmthp) OCIHandleFree(stmthp, OCI_HTYPE_STMT); } bool connect(const std::string& user, const std::string& password, const std::string& db) { OCIEnvCreate(&envhp, OCI_THREADED, nullptr, nullptr, nullptr, nullptr, 0, nullptr); OCIHandleAlloc(envhp, (void**)&errhp, OCI_HTYPE_ERROR, 0, nullptr); OCIHandleAlloc(envhp, (void**)&srvhp, OCI_HTYPE_SERVER, 0, nullptr); OCIHandleAlloc(envhp, (void**)&svchp, OCI_HTYPE_SVCCTX, 0, nullptr); OCILogon2(envhp, errhp, &svchp, (const OraText*)user.c_str(), user.length(), (const OraText*)password.c_str(), password.length(), (const OraText*)db.c_str(), db.length(), OCI_DEFAULT); return true; } void disconnect() { OCILogoff(svchp, errhp); } bool execute(const std::string& sql) { OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, nullptr); OCIStmtPrepare(stmthp, errhp, (const OraText*)sql.c_str(), sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT); OCIStmtExecute(svchp, stmthp, errhp, 1, 0, nullptr, nullptr, OCI_DEFAULT); return true; } int query(const std::string& sql) { OCIHandleAlloc(envhp, (void**)&stmthp, OCI_HTYPE_STMT, 0, nullptr); OCIStmtPrepare(stmthp, errhp, (const OraText*)sql.c_str(), sql.length(), OCI_NTV_SYNTAX, OCI_DEFAULT); OCIStmtExecute(svchp, stmthp, errhp, 0, 0, nullptr, nullptr, OCI_DEFAULT); int cols = 0; OCIAttrGet(stmthp, OCI_HTYPE_STMT, &cols, nullptr, OCI_ATTR_PARAM_COUNT, errhp); return cols; } private: OCIEnv* envhp; OCIError* errhp; OCIServer* srvhp; OCISvcCtx* svchp; OCIStmt* stmthp; }; int main() { OracleDB db; db.connect("user", "password", "db"); // 插入数据 db.execute("INSERT INTO table_name(col1, col2) VALUES('value1', 'value2')"); // 更新数据 db.execute("UPDATE table_name SET col1='new_value1' WHERE col2='value2'"); // 删除数据 db.execute("DELETE FROM table_name WHERE col1='new_value1'"); // 查询数据 int cols = db.query("SELECT * FROM table_name WHERE col2='value2'"); std::cout << "Number of columns: " << cols << std::endl; db.disconnect(); return 0; } ``` 这个类中,connect函数用于连接数据库;disconnect函数用于断开连接;execute函数用于执行insert、update、delete等操作;query函数用于执行select操作,并返回结果列数。这只是一个简单的实现,具体的实现方式可以根据实际需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小诺大人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值