使用Titanium Mobile开发iPhone/Android应用(10)- 数据库的使用
数据库的使用
这回我们说说使用数据库来操作数据。具体来说,就是我们在Twitter客户端的Home页面,取得Timeline数据后将其保存,这样一来我们就能在启动的时候显示前一次取得的Timeline数据,也能够检索Timeline中的内容。
取得Timeline
在最开始的页面上我们不仅仅显示自己的信息,也将Follow中的人的Timeline取出来显示。在以前我们做成的table_view.js中,关于Timeline取得部分的代码做如下修改:
Ti.include("lib/twitter_api.js");Ti.include("twitter_settings.js");Ti.include("tweet_db.js");//initializationTi.App.twitterApi = new TwitterApi({ consumerKey: TwitterSettings.consumerKey, consumerSecret: TwitterSettings.consumerSecret});var twitterApi = Ti.App.twitterApi;twitterApi.init(); twitterApi.statuses_home_timeline( { onSuccess: function(response){ updateTimeline(db.savedTweets()); }, onError: function(error){ Ti.API.error(error); } });
var TweetDB = function() { this.dbName = 'tweetdb'; //(1) this.open = function () { //(2) this.db = Titanium.Database.open(this.dbName); }; this.close = function () { //(3) this.db.close(); }; this.addTweets = function (tweets) { //(4) this.open(); for (var i=0;i<tweets.length;i++) { var tweet = tweets[i]; var rows = this.db.execute( //(5) 'SELECT * FROM tweets WHERE status_id = ?', tweet.id_str ); Ti.API.debug('Found: ' + rows.getRowCount() ); if ( rows.getRowCount() > 0 ) continue; var res = this.db.execute( 'INSERT INTO tweets (screen_name, profile_image_url, tweet_text, status_id, created_at) VALUES(?,?,?,?,?)', tweet.user.screen_name, tweet.user.profile_image_url, tweet.text, tweet.id_str, tweet.created_at ); Ti.API.debug('Add to DB'); } this.close(); return true; }; this.open(); this.db.execute('CREATE TABLE IF NOT EXISTS tweets (screen_name TEXT, profile_image_url TEXT, tweet_text TEXT, status_id TEXT, created_at TEXT)'); this.close();};
var rows = this.db.execute( 'SELECT * FROM tweets WHERE status_id = ?', tweet.id_str);Ti.API.debug('Found: ' + rows.getRowCount() );if ( rows.getRowCount() > 0 ) continue;
this.db.execute('CREATE TABLE IF NOT EXISTS tweets (screen_name TEXT, profile_image_url TEXT, tweet_text TEXT, status_id TEXT, created_at TEXT)');
var db = new TweetDB();twitterApi.statuses_home_timeline( { onSuccess: function(response){ db.addTweets(response); updateTimeline(response); }, onError: function(error){ Ti.API.error(error); } });
this.getSavedTweets = function() { this.open(); var rows = this.db.execute( 'SELECT * FROM tweets ORDER BY created_at DESC' ); //(1) var res = []; if ( rows.getRowCount() > 0 ) { while ( rows.isValidRow() ) { //(2) var tweetObj = {}; tweetObj.user = {}; tweetObj.user.screen_name = rows.fieldByName('screen_name'); tweetObj.user.profile_image_url = rows.fieldByName('profile_image_url'); tweetObj.text = rows.fieldByName('tweet_text'); var date = new Date(rows.fieldByName('created_at')); tweetObj.created_at = date.toLocaleString(); res.push(tweetObj); rows.next(); } } rows.close(); this.close(); return res;};
twitterApi.statuses_home_timeline( { onSuccess: function(response){ db.addTweets(response); updateTimeline(db.getSavedTweets()); }, onError: function(error){ Ti.API.error(error); } });
this.searchByScreenName = function (screen_name) { this.open(); var rows = this.db.execute( 'SELECT * FROM tweets WHERE screen_name like ?', '%' + screen_name + '%' ); var res = []; if ( rows.getRowCount() > 0 ) { while ( rows.isValidRow() ) { var tweetObj = {}; tweetObj.user = {}; tweetObj.user.screen_name = rows.fieldByName('screen_name'); tweetObj.user.profile_image_url = rows.fieldByName('profile_image_url'); tweetObj.text = rows.fieldByName('tweet_text'); var date = new Date(rows.fieldByName('created_at')); tweetObj.created_at = date.toLocaleString(); res.push(tweetObj); rows.next(); } } rows.close(); this.close(); return res.length ? res : null;;
var search = Titanium.UI.createSearchBar({ //(1) height:43, top:0});win1.add(search);search.addEventListener('return', function(e) //(2){ var query = e.value; var res = db.searchByScreenName(query); if (res) { updateTimeline(res); } search.blur(); search.hide();});search.addEventListener('change', function(e) //(3){ if (e.value.length == 0 ) { updateTimeline(db.savedTweets()); search.blur(); search.hide(); }});search.hide(); //(4)var searchButton = Ti.UI.createButton( //(5) { title: 'search' });searchButton.addEventListener( //(6) 'click', function () { search.show(); });win1.leftNavButton = searchButton;