瞢知青 發表於 2019-9-17 16:26:00

React Native 之react-native-sqlite-storage

<p>npm 官网指导:&nbsp;https://www.npmjs.com/package/react-native-sqlite-storage</p>
<p>1. 执行: npm install&nbsp;react-native-sqlite-storage</p>
<p>2.cd ios 执行pod install</p>
<p>3.执行react native link</p>
<p>使用:</p>
<p>按照上面官网说的,在ios项目根目录下新建www文件夹,里面放sqlite数据库文件</p>
<p>在react native项目中使用:&nbsp;</p>
<p>1. 导入 import SQLiteManager from 'react-native-sqlite-storage'</p>
<p>2.打开数据库:&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> db = SQLiteManager.openDatabase({name: "mydata.db", createFromLocation: 1}, openCB, errorCB); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">参数依次是:数据库名,路径,操作成功函数,操作失败函数</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> openCB() {
    console.log(</span>'open!'<span style="color: rgba(0, 0, 0, 1)">)
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> errorCB(err) {
    console.log(err)
}</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
*
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> closeDB(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(db){
    db.close()

}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {

}
}</span></pre>
</div>
<p>&nbsp;</p>
<p>3.创建表:</p>
<div class="cnblogs_code">
<pre>db.transaction((tx) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建表</span>
    tx.executeSql('CREATE TABLE IF NOT EXISTS DATA(' +
                'id INTEGER PRIMARY KEYAUTOINCREMENT,' +
                'title VARCHAR,'+
                'value VARCHAR,' +
                'time VARCHAR,' +
                'year VARCHAR,' +
                'month VARCHAR,' +
                'date VARCHAR)',[],() =&gt; {Alert.alert('createTable executeSql success'<span style="color: rgba(0, 0, 0, 1)">)},
    (err) </span>=&gt; { Alert.alert('createTableexecuteSql error='<span style="color: rgba(0, 0, 0, 1)">,err)})

}
)</span></pre>
</div>
<p>4.查询</p>
<div class="cnblogs_code">
<pre>db.transaction((tx) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            tx.executeSql(select, [], (tx, result) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

               
                let arr </span>=<span style="color: rgba(0, 0, 0, 1)"> []
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; result.rows.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
                  
                  arr.push(result.rows.item(i))
                }
               
                </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
            

                  dataSource: arr

                })



            })
      });</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">select 是sql语句</span></pre>
</div>
<p>&nbsp;</p>
<h1 class="_1RuRku">RN- react-native-sqlite-storage 封装增删改查方法:</h1>
<p>https://www.jianshu.com/p/69a2e7e93caf</p>
<p>&nbsp;</p>
<p>封装好的工具:https://linux.ctolib.com/NikiLee2016-react-native-sqlite-helper-pro.html</p>
<h1 id="articleHeader0">react-native-sqlite-helper-pro</h1>

</div>
<div id="MySignature" role="contentinfo">
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.                        

我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/<br><br>
来源:https://www.cnblogs.com/liuw-flexi/p/11534893.html
頁: [1]
查看完整版本: React Native 之react-native-sqlite-storage