Use SQLite in Android development to implement news collection and cancellation features _android

Source: Internet
Author: User
Tags sqlite sqlite database sonatype


Prior to learning Oracle, simply that the database exists only on the server side, learning Android only to find that Android and iOS themselves are "carry" the database--sqlite, is a lightweight, embedded, relational database, is Android, A widely used database system such as iOS. Used to store the local state. Just write a realization of the function of the news collection, written for your reference.



In Android we manipulate SQLite databases by Sqlitedatabase objects of this class. Because the SQLite database does not need to establish the connection as the C/s database and the characteristics of the authentication, as well as the characteristics of the SQLite database single file database, it makes it easy to obtain the Sqlitedatabase object as the object of the operation file.



SQLite to create a database, create a table, and then make a check and other features. So the first step is to create a database that Sqliteopenhelper is an abstract class that manages the creation and versioning of databases. To use it you must implement its ncreate (Sqlitedatabase), Onupgrade (sqlitedatabase, int, int) methods


# Confirm that JRE8 is installed
# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

# Create a nexus user and set the user's File Handle Limits
# useradd nexus
# echo "nexus-nofile 65536" >> /etc/security/limits.conf

# Download and decompress nexus to the / opt directory, and set nexus user permissions
# wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
# tar -xzvf latest-unix.tar.gz -C / opt
# mv / opt / nexus * / opt / nexus
# chown -R nexus: nexus / opt / nexus / opt / sonatype-work /

# Set service startup user
# echo 'run_as_user = "nexus"'> /opt/nexus/bin/nexus.rc

# Use systemd management service here
# cat << EOF> /etc/systemd/system/nexus.service
[Unit]
Description = nexus service
After = network.target

[Service]
Type = forking
ExecStart = / opt / nexus / bin / nexus start
ExecStop = / opt / nexus / bin / nexus stop
User = nexus
Restart = on-abort

[Install]
WantedBy = multi-user.target
EOF

# systemctl daemon-reload
# systemctl enable nexus.service
# systemctl start nexus.service
 
# Finally, check the log for service running status
# tail -f /opt/sonatype-work/nexus3/log/nexus.log


Android provides a class called Sqlitedatabase that encapsulates some of the APIs that manipulate the database. It enables basic CRUD operations, with Getwritabledatabase () and Getreadabledatabase () to get database instances, and you can write DAO-layer methods to manipulate the table:


publicclass DetailNewsDao {
private NewsDBHelper helper;
public DetailNewsDao (Context context) {
helper = new NewsDBHelper (context); // Establish a connection with the database}
// Insert data
publicvoid insertDetsilNews (News news) {
SQLiteDatabase db = helper.getWritableDatabase ();
db.execSQL ("insert into news (_title, _url, _docid)" + // The title of the news to be collected is titled, identifies the docid, and the detailed address url is transferred to the database. Then you can open the news in detail. ,?) ", new String [] {news.getTitle (), news.getDetailUrl (), news.getDocid ()});
db.close ();
}
//delete data
publicvoid del (String docid) {// Delete data according to the incoming parameter docid SQLiteDatabase db = helper.getReadableDatabase ();
db.execSQL ("delete from news where _docid =?", new Object [] {docid});
db.close ();
}
//Query data
public List <News> findSelected () {
SQLiteDatabase db = helper.getReadableDatabase ();
Cursor c = db.rawQuery ("select * from news", null); // Use rawQuery () only when querying data, add, delete, modify, and create tables, all use execSQl () List <News> list = new ArrayList <News> ();
while (c.moveToNext ()) {
News news = new News ();
news.setTitle (c.getString (c.getColumnIndex ("_ title")));
news.setDetailUrl (c.getString (c.getColumnIndex ("_ url")));
news.setDocid (c.getString (c.getColumnIndex ("_ docid")));
list.add (news);
}
c.close ();
db.close ();
return list;
}
}





Use Actionbar to do menu buttons, click Favorites, execute code:


if (item.getTitle (). equals ("Favorites"))) {
Toast.makeText (this, "Collection successfully", Toast.LENGTH_LONG) .show ();
detailNewsDao.insertDetsilNews (news);
item.setTitle ("Unfavorite");
} else {
detailNewsDao.del (news.getDocid ());
Toast.makeText (this, "Uncollect", Toast.LENGTH_LONG) .show ();
item.setTitle ("Collection");
} 


The above is a small set to introduce the Android development using SQLite to achieve news collection and cancellation of collection functions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.