Java: The Path to the lion (Android) -- SQLite, android -- sqlite

Source: Internet
Author: User

Java: The Path to the lion (Android) -- SQLite, android -- sqlite

I. Junit
1. How to Use
Configure in the AndroidManifest. xml file, configure instrumentation under the manifest borrow point, and configure uses-library under the application borrow point.
Define class inheritance AndroidTestCast
Define the test method, Run As JunitTest
To determine the test result, use Assert. assertEquals.

The following is an independent test project JunitTest to test the project Junit:

Package com. shellway. junit; public class Service {public int divide (int a, int B) {return a/B ;}}Service. javapackage com. shellway. junit; import junit. framework. assert; import android. test. androidTestCase; public class TestT extends AndroidTestCase {public void test1 () {Service service Service = new Service (); System. out. println (service. divide (10, 2);} public void test2 () {Service service = new Service (); System. out. println (service. divide (10, 0);} public void test3 () {Service service = new Service (); Assert. assertEquals (2.5, service. divide (10, 4 ));}}TestT. java <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. shellway. junit "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 16 "android: targetSdkVersion = "21"/> <instrumentation android: targetPackage = "com. shellway. junit "android: name =" android. test. instrumentationTestRunner "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <uses-library android: name = "android. test. runner "/> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>AndroidManifest. xml

In the JunitTest project:

Package com. shellway. junit. test; import junit. framework. assert; import com. shellway. junit. service; import android. test. androidTestCase; public class MyTest extends AndroidTestCase {public void test1 () {Service service Service = new Service (); System. out. println (service. divide (10, 2);} public void test2 () {Service service = new Service (); System. out. println (service. divide (10, 0);} public void test3 () {Service service = new Service (); Assert. assertEquals (2.5, service. divide (10, 4 ));}}MyTest. java <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. shellway. junit. test "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 16 "/> <instrumentation android: name =" android. test. instrumentationTestRunner "android: targetPackage =" com. shellway. junit "/> <application android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "> <uses-library android: name =" android. test. runner "/> </application> </manifest>AndroidManifest. xml


Ii. Logs
1. How to Use
Log. v (), d (), I (), w (), e ()
Date information can be output by level, and tags can be specified

Package com. example. logcat; import android. test. androidTestCase; import android. util. log; public class LogTest extends AndroidTestCase {public void test1 () {System. out. println ("System. out "); System. err. println ("System. out ");} public void test2 () {Log. v ("LogTest", "verbose"); Log. d ("LogTest", "debug"); Log. I ("LogTest", "info"); Log. w ("LogTest", "warning"); Log. e ("LogTest", "error ");}}LogTest. java <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. logcat "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion = "21"/> <instrumentation android: targetPackage = "com. example. logcat "android: name =" android. test. instrumentationTestRunner "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <uses-library android: name = "android. test. runner "/> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>AndroidManifest. xml


3. Read and Write files
1. read/write SD card
To obtain the SD card path, use Environment. getExternalStorageDirectory ()
It is best to determine the status before using the SD card, Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED)
Permission required for writing to SD card, android. permission. WRITE_EXTERNAL_STORAGE
2. Read and Write mobile phones
You can use Context. openFileOutput (String, int) to open the output stream.
After the file name is specified, the files folder is automatically generated in the folder where the current application is located.
The int parameter is a File Permission and can be set using constants in the Context.

Iv. SharedPreferences
1. What is SharedPreferences?
Is a key-Value Pair structure container, similar to Map (Properties), where you can store key-value pairs. Based on the key search value, the data stored in the container is saved as an xml file.
2. How to Use
Use Context. getSharedPreferences (String, int) to get the object and specify the file name and file mode.
Use the SharedPreferences. edit () method to obtain the Editor.
Use Editor. putString (), putInt (), and other methods to store data
Use the Editor. commit () method to submit changes (similar transactions)
You can use SharedPreferences. getString (), getInt (), and other methods to obtain data.

V. XML
1. Resolution
Get Parser: Xml. newPullParser ()
Set the input stream: parser. setInput (InputStream, String)
Get the current event type: parser. getEventType (), 5 in total
Obtain the next event type: parser. next ()
Get Tag Name: parser. getName ()
Get property value: parser. getAttributeValue (int)
Get the next text: parser. nextText ()
2. Generate
Get Parser:
Set the output stream:
Start document:
End document:
Enable tags:
End Tag:
Set attributes:
Set text:

6. SQLite Database
1. Features of SQLite Database
The database provided by the mobile phone does not distinguish the data type (except the primary key). The syntax is the same as that of MySQL. Each database is a file.
2. Create a database
The definition class inherits SQLiteOpenHelper, defines the constructor, explicitly calls the parent class constructor, and passes in four parameters.
Override onCreate () and onUpgrade () Methods
You can call getWritableDatabase () or getReadableDatabase () to create a database.
If the database file does not exist, the database file is created and the onCreate () method is executed.
If the database file exists and the version is not changed, no method is executed.
If the database file exists and the version is upgraded, run the onUpgrade () method.
3. add, delete, modify, and query
You can use sqlitedatabase.exe cSQL () to execute the SQL statement.
You must use the SQLiteDatabase. rawQuery () method to query the data, obtain a Cursor, and call methods such as moveToNext () and getString () getInt () to obtain data.


In java, how can I use cmd to query the android sqlite database?

Rt.exe c ("cmd.exe/c adb shell sqlite3/data/com. test/database/test. db & select * from user "); maybe cmd has to go to the abd database installation directory so that you can only write the command first. in the bat file, use cmd to call this operation. Bat

Java generates the Sqlite database and downloads it to the Android device. An error is returned, indicating that the version number is incorrect.

If the mobile phone is java software, you need to install java software on the mobile phone, but almost all mobile phones support it, it's just a different version. · if you want to know how to use java after learning it, java is better at web page development and now popular Android technology ··

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.