Latest 1.0.5
Homepage https://github.com/Tencent/wcdb
License BSD
Platforms ios 7.0, osx 10.9, requires ARC
Frameworks CoreFoundation, Security, Foundation
Authors

PRs Welcome
Release Version
WeChat Approved iOS
WeChat Approved Android
Platform

中文版本请参看这里

WCDB is an efficient, complete, easy-to-use mobile database framework used in the WeChat application. It’s currently available on iOS, macOS and Android.

WCDB for iOS/macOS

Features

  • Easy-to-use. Through WCDB, you can get objects from database in one line code.

    • WINQ (WCDB language integrated query): WINQ is a native data querying capability which frees developers from writing glue code to concatenate SQL query strings.

    • ORM (Object Relational Mapping): WCDB provides a flexible, easy-to-use ORM for creating tables, indices and constraints, as well as CRUD through ObjC objects.
    [database getObjectsOfClass:WCTSampleConvenient.class
                      fromTable:tableName
                          where:WCTSampleConvenient.intValue>=10
                          limit:20];
  • Efficient. Through the framework layer and sqlcipher source optimization, WCDB have more efficient performance.

    • Multi-threaded concurrency: WCDB supports concurrent read-read and read-write access via connection pooling.
    • Batch Write Performance Test.

      For more benchmark data, please refer to our benchmark.
  • Complete.

    • Encryption Support: WCDB supports database encryption via SQLCipher.
    • Corruption recovery: WCDB provides a built-in repair kit for database corruption recovery.
    • Anti-injection: WCDB provides a built-in protection from SQL injection.

Getting Started

Prerequisites

  • Apps using WCDB can target: iOS 7 or later, macOS 10.9 or later.
  • Xcode 8.0 or later required.
  • Objective-C++ required.

Installation

  • Via Cocoapods:
    1. Install CocoaPods.
    2. Run pod repo update to make CocoaPods aware of the latest available WCDB versions.
    3. In your Podfile, add pod 'WCDB' to your app target.
    4. From the command line, run pod install.
    5. Use the .xcworkspace file generated by CocoaPods to work on your project!
    6. Add #import <WCDB/WCDB.h> at the top of your Objective-C++ source files and start your WCDB journey.
    7. **Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm.
  • Via Carthage:
    1. Install Carthage;
    2. Add github "Tencent/WCDB" to your Cartfile;
    3. Run carthage update.
    4. Drag WCDB.framework from the appropriate platform directory in Carthage/Build/ to the Linked Binary and Libraries section of your Xcode project’s Build Phases settings;
    5. On your application targets’ Build Phases settings tab, click the "+" icon and choose New Run Script Phase. Create a Run Script with carthage copy-frameworks and add the paths to the frameworks under Input Files: $(SRCROOT)/Carthage/Build/iOS/WCDB.framework or $(SRCROOT)/Carthage/Build/Mac/WCDB.framework;
    6. Add #import <WCDB/WCDB.h> at the top of your Objective-C++ source files and start your WCDB journey.
    7. Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm.
  • Via Dynamic Framework:
    Note that Dynamic frameworks are not compatible with iOS 7. See “Static Framework” for iOS 7 support.

    1. Getting source code from git repository and update the submodule of sqlcipher.
      • git clone https://github.com/Tencent/wcdb.git
      • cd wcdb
      • git submodule update --init sqlcipher
    2. Drag WCDB.xcodeproj in wcdb/apple/ into your project;
    3. Add WCDB.framework to the Enbedded Binaries section of your Xcode project’s General settings; Note that there are two frameworks here and the dynamic one should be chosen. You can check it at Build Phases->Target Dependencies. The right one is WCDB while `WCDB iOS Static is used for static lib.
    4. Add #import <WCDB/WCDB.h> at the top of your Objective-C++ source files and start your WCDB journey.
    5. Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm.
  • Via Static Framework:
    1. Getting source code from git repository and update the submodule of sqlcipher.
      • git clone https://github.com/Tencent/wcdb.git
      • cd wcdb
      • git submodule update --init sqlcipher
    2. Drag WCDB.xcodeproj in wcdb/apple/ into your project;
    3. Add WCDB iOS Static to the Target Dependencies section of your Xcode project’s Build Phases settings;
    4. Add WCDB.frameworklibz.tbd to the Linked Binary and Libraries section of your Xcode project’s Build Phases settings; Note that there are two WCDB.framework, you should choose the one from WCDB iOS Static target.
    5. Add -all_load and -ObjC to the Other Linker Flags section of your Xcode project’s Build Settings.
    6. Add #import <WCDB/WCDB.h> at the top of your Objective-C++ source files and start your WCDB journey.
    7. Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm.

Tutorials

Tutorials can be found here.

Documentations

  • Documentations can be found on our Wiki.
  • API references for iOS/macOS can be found here.
  • Performence data can be found on our benchmark.

WCDB for Android

Features

  • Database encryption via SQLCipher.
  • Concurrent access via connection pooling from modern Android framework.
  • Repair toolkit for database corruption recovery.
  • Database backup and recovery utility optimized for small backup size.
  • Log redirection and various tracing facilities.
  • API 12 (Android 3.1) and above are supported.

Getting Started

To include WCDB to your project, choose either way: import via Maven or via AAR package.

Import via Maven

To import WCDB via Maven repositories, add the following lines to build.gradle on your
app module:

dependencies {
    compile 'com.tencent.wcdb:wcdb-android:1.0.5'
    // Replace "1.0.2" to any available version.
}

This will cause Gradle to download AAR package from jcenter while building your application.

Import Prebuilt AAR Package

  1. Download AAR package from release page.
  2. Import the AAR as new module. In Android Studio, select File -> New -> New Module... menu and choose "Import JAR/AAR Package".
  3. Add a dependency on the new module. This can be done using File -> Project Structure... in Android Studio, or by adding following code to application’s build.gradle:

dependencies {
    // Change "wcdb" to the actual module name specified in step 2.
    compile project(':wcdb')
}

Migrate from Plain-text SQLite Databases

WCDB has interfaces very similar to Android SQLite Database APIs. To migrate you application from
AOSP API, change import path from android.database.* to com.tencent.wcdb.*, and
android.database.sqlite.* to com.tencent.wcdb.database.*. After import path update,
your application links to WCDB instead of AOSP API.

To open or create an encrypted database, use with-password versions of
SQLiteDatabase.openOrCreateDatabase(), SQLiteOpenHelper.getWritableDatabase(),
or Context.openOrCreateDatabase().

Note: WCDB uses byte[] for password instead of String in SQLCipher Android Binding.

String password = "MyPassword";
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/path/to/database", password.getBytes(), 
        null, null);

See sample-encryptdb for sample for transferring data between plain-text and encrypted
databases.

Corruption Recovery

See sample-repairdb for instructions how to recover corrupted databases using RepairKit.

Redirect Log Output

By default, WCDB prints its log message to system logcat. You may want to change this
behavior in order to, for example, save logs for troubleshooting. WCDB can redirect
all of its log outputs to user-defined routine using Log.setLogger(LogCallback)
method.

Build from Sources

Build WCDB Android with Prebuilt Dependencies

WCDB itself can be built apart from its dependencies using Gradle or Android Studio.
To build WCDB Android library, run Gradle on android directory:

$ cd android
$ ./gradlew build

Building WCDB requires Android NDK installed. If Gradle failed to find your SDK and/or
NDK, you may need to create a file named local.properties on the android directory
with content:

sdk.dir=path/to/sdk
ndk.dir=path/to/ndk

Android Studio will do this for you when the project is imported.

Build Dependencies from Sources

WCDB depends on OpenSSL crypto library and SQLCipher. You can rebuild all dependencies
if you wish. In this case, a working C compiler on the host system, Perl 5, Tcl and a
bash environment is needed to be installed on your system.

To build dependencies, checkout all submodules, set ANDROID_NDK_ROOT environment
variable to your NDK path, then run build-depends-android.sh:

$ export ANDROID_NDK_ROOT=/path/to/ndk
$ ./build-depends-android.sh

This will build OpenSSL crypto library and generate SQLCipher amalgamation sources
and place them to proper locations suitable for WCDB library building.

Documentations

  • Documentations can be found on our Wiki.
  • API references for Android can be found here.

Latest podspec

{
    "name": "WCDB",
    "version": "1.0.5",
    "summary": "WCDB is a cross-platform database framework developed by Wechat.",
    "description": "WCDB is an efficient, complete, easy-to-use mobile database framework used in the WeChat application.nIt can be a replacement for Core Data, SQLite & FMDB.",
    "homepage": "https://github.com/Tencent/wcdb",
    "license": {
        "type": "BSD",
        "file": "LICENSE"
    },
    "authors": {
        "sanhuazhang": "[email protected]"
    },
    "platforms": {
        "ios": "7.0",
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/Tencent/wcdb.git",
        "tag": "v1.0.5"
    },
    "public_header_files": [
        "apple/WCDB/WCDB.h",
        "apple/WCDB/**/*.{h,hpp}"
    ],
    "source_files": [
        "apple/WCDB/WCDB.h",
        "apple/WCDB/**/*.{h,m,hpp,cpp,mm}",
        "repair",
        "sqlcipher/keywordhash.h",
        "sqlcipher/src/callback.c",
        "sqlcipher/src/loadext.c",
        "sqlcipher/src/rowset.c",
        "sqlcipher/src/treeview.c",
        "sqlcipher/ext/userauth.c",
        "sqlcipher/src/vtab.c",
        "sqlcipher/src/btmutex.c",
        "sqlcipher/src/btree.c",
        "sqlcipher/src/btreeInt.h",
        "sqlcipher/src/btree.h",
        "sqlcipher/fts5.c",
        "sqlcipher/fts5.h",
        "sqlcipher/ext/fts3/fts3_aux.c",
        "sqlcipher/ext/fts3/fts3_expr.c",
        "sqlcipher/ext/fts3/fts3_hash.c",
        "sqlcipher/ext/fts3/fts3_hash.h",
        "sqlcipher/ext/fts3/fts3_icu.c",
        "sqlcipher/ext/fts3/fts3_porter.c",
        "sqlcipher/ext/fts3/fts3_snippet.c",
        "sqlcipher/ext/fts3/fts3_tokenize_vtab.c",
        "sqlcipher/ext/fts3/fts3_tokenizer.c",
        "sqlcipher/ext/fts3/fts3_tokenizer1.c",
        "sqlcipher/ext/fts3/fts3_unicode.c",
        "sqlcipher/ext/fts3/fts3_unicode2.c",
        "sqlcipher/ext/fts3/fts3_write.c",
        "sqlcipher/ext/fts3/fts3.c",
        "sqlcipher/ext/fts3/fts3.h",
        "sqlcipher/ext/fts3/fts3Int.h",
        "sqlcipher/src/backup.c",
        "sqlcipher/src/legacy.c",
        "sqlcipher/src/main.c",
        "sqlcipher/src/notify.c",
        "sqlcipher/src/vdbeapi.c",
        "sqlcipher/src/table.c",
        "sqlcipher/src/wal.c",
        "sqlcipher/src/wal.h",
        "sqlcipher/src/status.c",
        "sqlcipher/src/prepare.c",
        "sqlcipher/src/malloc.c",
        "sqlcipher/src/mem0.c",
        "sqlcipher/src/mem1.c",
        "sqlcipher/src/mem2.c",
        "sqlcipher/src/mem3.c",
        "sqlcipher/src/mem5.c",
        "sqlcipher/src/memjournal.c",
        "sqlcipher/src/mutex_unix.c",
        "sqlcipher/src/mutex_noop.c",
        "sqlcipher/src/mutex.c",
        "sqlcipher/src/mutex.h",
        "sqlcipher/src/os_common.h",
        "sqlcipher/src/os_setup.h",
        "sqlcipher/src/os_unix.c",
        "sqlcipher/src/queue.c",
        "sqlcipher/src/queue.h",
        "sqlcipher/src/os_wcdb.c",
        "sqlcipher/src/os_wcdb.h",
        "sqlcipher/src/mutex_wcdb.c",
        "sqlcipher/src/mutex_wcdb.h",
        "sqlcipher/src/os.c",
        "sqlcipher/src/os.h",
        "sqlcipher/src/threads.c",
        "sqlcipher/src/bitvec.c",
        "sqlcipher/src/pager.c",
        "sqlcipher/src/pager.h",
        "sqlcipher/src/pcache.c",
        "sqlcipher/src/pcache.h",
        "sqlcipher/src/pcache1.c",
        "sqlcipher/ext/rtree/rtree.c",
        "sqlcipher/ext/rtree/rtree.h",
        "sqlcipher/ext/rtree/sqlite3rtree.h",
        "sqlcipher/src/complete.c",
        "sqlcipher/src/tokenize.c",
        "sqlcipher/src/resolve.c",
        "sqlcipher/parse.c",
        "sqlcipher/parse.h",
        "sqlcipher/src/analyze.c",
        "sqlcipher/src/func.c",
        "sqlcipher/src/wherecode.c",
        "sqlcipher/src/whereexpr.c",
        "sqlcipher/src/whereInt.h",
        "sqlcipher/src/alter.c",
        "sqlcipher/src/attach.c",
        "sqlcipher/src/auth.c",
        "sqlcipher/src/build.c",
        "sqlcipher/src/delete.c",
        "sqlcipher/src/expr.c",
        "sqlcipher/src/insert.c",
        "sqlcipher/src/pragma.c",
        "sqlcipher/src/pragma.h",
        "sqlcipher/src/select.c",
        "sqlcipher/src/trigger.c",
        "sqlcipher/src/update.c",
        "sqlcipher/src/vacuum.c",
        "sqlcipher/src/walker.c",
        "sqlcipher/src/where.c",
        "sqlcipher/opcodes.c",
        "sqlcipher/opcodes.h",
        "sqlcipher/src/sqlcipher.h",
        "sqlcipher/sqlite3.h",
        "sqlcipher/ext/rbu/sqlite3rbu.c",
        "sqlcipher/ext/rbu/sqlite3rbu.h",
        "sqlcipher/ext/userauth/sqlite3userauth.h",
        "sqlcipher/ext/misu/json1.c",
        "sqlcipher/ext/icu/icu.c",
        "sqlcipher/ext/icu/sqliteicu.h",
        "sqlcipher/src/global.c",
        "sqlcipher/src/ctime.c",
        "sqlcipher/src/hwtime.h",
        "sqlcipher/src/date.c",
        "sqlcipher/src/dbstat.c",
        "sqlcipher/src/fault.c",
        "sqlcipher/src/fkey.c",
        "sqlcipher/src/sqliteInt.h",
        "sqlcipher/src/sqliteLimit.h",
        "sqlcipher/src/sqlite3ext.h",
        "sqlcipher/src/hash.c",
        "sqlcipher/src/hash.h",
        "sqlcipher/src/printf.c",
        "sqlcipher/src/random.c",
        "sqlcipher/src/utf.c",
        "sqlcipher/src/util.c",
        "sqlcipher/src/crypto_cc.c",
        "sqlcipher/src/crypto_impl.c",
        "sqlcipher/src/crypto_libtomcrypt.c",
        "sqlcipher/src/crypto.c",
        "sqlcipher/src/crypto.h",
        "sqlcipher/src/vdbe.c",
        "sqlcipher/src/vdbe.h",
        "sqlcipher/src/vdbeaux.c",
        "sqlcipher/src/vdbeblob.c",
        "sqlcipher/src/vdbeInt.h",
        "sqlcipher/src/vdbemem.c",
        "sqlcipher/src/vdbesort.c",
        "sqlcipher/src/vdbetrace.c",
        "sqlcipher/src/msvc.h",
        "sqlcipher/src/vxworks.h",
        "sqlcipher/fts3_tokenizer.h"
    ],
    "frameworks": [
        "CoreFoundation",
        "Security",
        "Foundation"
    ],
    "ios": {
        "frameworks": "UIKit"
    },
    "libraries": [
        "z",
        "c++"
    ],
    "requires_arc": true,
    "prepare_command": "git submodule update --init sqlcipher;                           cd apple/xctemplate; make; cd ../..;                           cd sqlcipher; make -f Makefile.preprocessed; cd ..;                           cp sqlcipher/ext/fts3/fts3_tokenizer.h sqlcipher/",
    "compiler_flags": "-DWCDB_COCOAPODS",
    "xcconfig": {
        "GCC_PREPROCESSOR_DEFINITIONS": "$(inherited) WCDB_BUILTIN_COLUMN_CODING SQLITE_ENABLE_FTS3 SQLITE_ENABLE_FTS3_PARENTHESIS SQLITE_ENABLE_API_ARMOR SQLITE_OMIT_BUILTIN_TEST SQLITE_OMIT_AUTORESET SQLITE_ENABLE_UPDATE_DELETE_LIMIT SQLITE_ENABLE_RTREE SQLITE_ENABLE_LOCKING_STYLE=1 SQLITE_SYSTEM_MALLOC SQLITE_OMIT_LOAD_EXTENSION SQLITE_CORE SQLITE_THREADSAFE=2 SQLITE_DEFAULT_CACHE_SIZE=250 SQLITE_DEFAULT_CKPTFULLFSYNC=1 SQLITE_DEFAULT_PAGE_SIZE=4096 SQLITE_OMIT_SHARED_CACHE SQLITE_HAS_CODEC SQLCIPHER_CRYPTO_CC USE_PREAD=1 SQLITE_TEMP_STORE=2 SQLCIPHER_PREPROCESSED HAVE_USLEEP SQLITE_MALLOC_SOFT_LIMIT=0 SQLITE_WCDB_SIGNAL_RETRY=1 SQLITE_DEFAULT_MEMSTATUS=0 SQLITE_ENABLE_COLUMN_METADATA SQLITE_DEFAULT_WAL_SYNCHRONOUS=1 SQLITE_LIKE_DOESNT_MATCH_BLOBS SQLITE_MAX_EXPR_DEPTH=0 SQLITE_OMIT_DEPRECATED SQLITE_OMIT_PROGRESS_CALLBACK SQLITE_OMIT_SHARED_CACHE OMIT_CONSTTIME_MEM OMIT_MEMLOCK",
        "CLANG_WARN_CONSTANT_CONVERSION": "YES",
        "HEADER_SEARCH_PATHS": "$(inherited) ${PODS_ROOT}/WCDB",
        "LIBRARY_SEARCH_PATHS[sdk=macosx*]": "$(inherited) $(SDKROOT)/usr/lib/system",
        "CLANG_CXX_LANGUAGE_STANDARD": "gnu++0x",
        "CLANG_CXX_LIBRARY": "libc++",
        "GCC_WARN_64_TO_32_BIT_CONVERSION": "NO",
        "CLANG_WARN_UNREACHABLE_CODE": "NO",
        "GCC_WARN_UNUSED_FUNCTION": "NO",
        "GCC_WARN_UNUSED_VARIABLE": "NO"
    }
}

Pin It on Pinterest

Share This