1、配置LitePal
dependencies {
compile fileTree(
dir
:
'libs'
,
include
: [
'*.jar'
])
testCompile
'junit:junit:4.12'
compile
'com.android.support:appcompat-v7:24.2.1'
compile
'org.litepal.android:core:1.3.2'
}
2、在AndroidManifest.xml中配置:
<!--android:name="org.litepal.LitePalApplication" 使用orm框架 LitePal -->
<
application
android:name="org.litepal.LitePalApplication"
android
:allowBackup=
"true"
android
:icon=
"@mipmap/ic_launcher"
android
:label=
"@string/app_name"
android
:supportsRtl=
"true"
android
:theme=
"@style/AppTheme"
>
<
activity
android
:name=
".MainActivity"
>
<
intent-filter
>
<
action
android
:name=
"android.intent.action.MAIN"
/>
<
category
android
:name=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
3、建立main/assets/litepal.xml
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<
litepal
>
<!-- dbname 指定数据库名字 -->
<
dbname
value=
"BookStore"
></
dbname
>
<
version
value=
"1"
></
version
>
<!--在list中配置orm框架对应的 类 -->
<
list
>
<
mapping
class=
"com.example.litepaltest.Book"
></
mapping
>
<
mapping
class=
"com.example.litepaltest.Category"
></
mapping
>
</
list
>
</
litepal
>
4、建立pojo
package
com.example.litepaltest;
import
org.litepal.crud.DataSupport;
public class
Book
extends
DataSupport {
private int
id
;
private
String
author
;
private double
price
;
private int
pages
;
public int
getId() {
return
id
;
}
public void
setId(
int
id) {
this
.
id
= id;
}
public
String getAuthor() {
return
author
;
}
public void
setAuthor(String author) {
this
.
author
= author;
}
public double
getPrice() {
return
price
;
}
public void
setPrice(
double
price) {
this
.
price
= price;
}
public int
getPages() {
return
pages
;
}
public void
setPages(
int
pages) {
this
.
pages
= pages;
}
}
5、操作:
package
com.example.litepaltest;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.Button;
import
org.litepal.crud.DataSupport;
import
org.litepal.tablemanager.Connector;
import
java.util.List;
public class
MainActivity
extends
AppCompatActivity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
Button createDatabase = (Button) findViewById(R.id.
create_database
);
createDatabase.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
/*这一句话就已经创建了database 更改表结构之后,
只需在xml中重新配置一下版本号,运行getDatabase就行了,不需要drop表*/
Connector.
getDatabase
();
}
});
Button addData = (Button) findViewById(R.id.
add_data
);
addData.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
Book book =
new
Book();
book.setName(
"The Da Vinci Code"
);
book.setAuthor(
"Dan Brown"
);
book.setPages(
454
);
book.setPrice(
16.96
);
book.setPress(
"Unknow"
);
book.save();
}
});
Button updateData = (Button) findViewById(R.id.
update_data
);
updateData.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
Book book =
new
Book();
/*如果想设置成默认值,那么只能使用这种方式: book.setToDefault("pages"); */
book.setPrice(
14.95
);
book.setPress(
"Anchor"
);
/*updataAll如果不指定约束条件,那么就会对所有的数据起作用*/
book.updateAll(
"name = ? and author = ?"
,
"The Lost Symbol"
,
"Dan Brown"
);
}
});
Button deleteButton = (Button) findViewById(R.id.
delete_data
);
deleteButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
/*deleteAll如果不指定 后面两个约束条件 ,那么就会对所有的数据起作用*/
DataSupport.
deleteAll
(Book.
class
,
"price < ?"
,
"15"
);
}
});
/*查询*/
Button queryButton = (Button) findViewById(R.id.
query_data
);
queryButton.setOnClickListener(
new
View.OnClickListener() {
@Override
public void
onClick(View v) {
List<Book> books = DataSupport.
findAll
(Book.
class
);
for
(Book book: books) {
Log.
d
(
"MainActivity"
,
"book name is "
+ book.getName());
Log.
d
(
"MainActivity"
,
"book author is "
+ book.getAuthor());
Log.
d
(
"MainActivity"
,
"book pages is "
+ book.getPages());
Log.
d
(
"MainActivity"
,
"book price is "
+ book.getPrice());
Log.
d
(
"MainActivity"
,
"book press is "
+ book.getPress());
}
}
});
}
}
6、查询扩展: