//DBAdapter
package com.example.sqlitetext;import java.security.PublicKey;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
private static final String db_name="location.db";
private static final String db_table="locationinfo";
private static final int db_version=1;//更新数据库,修改标本号
public static final String key_id="_id";
public static final String latitude="latitude";//37.422006
public static final String longtitude="longtitude";//-122.084095
private SQLiteDatabase db;
private Context context;
private DBOpenHelper dbOpenhelper;
/***
* 内部类:实现数据库的创建、获取读写操作的类,静态类,可以直接调用
* @author Administrator
*
*/
private static class DBOpenHelper extends SQLiteOpenHelper{
public DBOpenHelper(Context context, String name, //构造函数
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
private static final String db_create="create table "+db_table+
"("+key_id+ " integer primary key autoincrement, "+
longtitude+" text not null, "+latitude+" text not null)";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(db_create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+db_table);//如果数据库存就删除
onCreate(db); //新建数据库
}};
public DBAdapter(Context _context) {//构造函数
// TODO Auto-generated constructor stu
context=_context;
}
/***
* 获取数据库打开的方法
* @throws SQLiteException
*/
public void open() throws SQLiteException{
dbOpenhelper=new DBOpenHelper(context, db_name, null, db_version);//初始化版本号为1
try {
db=dbOpenhelper.getWritableDatabase();
} catch (SQLiteException e) {
// TODO: handle exception
db=dbOpenhelper.getReadableDatabase();
}
}
/***
* 关闭数据库
*/
public void close(){
if(db!=null){
db.close();
db=null;
}
}
//数据库操作方法
/***
* 添加 单个地理位置信息
* @param location
* @return
*/
public long insert(Location location){
ContentValues newValues=new ContentValues();
String aa=location.getLatitude();
String bb=location.getLongtitude();
newValues.put(longtitude, location.getLongtitude());
newValues.put(latitude, location.getLatitude());
return db.insert(db_table, null, newValues);
}
//数据库的所有操作方法
/***
* 删除 所有数据
* @return
*/
public long deleteAllData(){
return db.delete(db_table, null, null);//返回受影响行数
}
/***
* 删除 单条记录
* @param id
* @return
*/
public long deleteOneData(long id){
return db.delete(db_table, key_id+"="+id, null);
}
/***
* 查询 所有记录
* @return
*/
public Location[] queryAllLocations(){
Cursor results=db.query(db_table, new String[]{key_id,longtitude,
latitude}, null, null, null, null,null);
return ConvertToLocations(results);
}
/***
* 查询 单条记录
* @return
*/
public Location[] queryOneLocation(long id){
Cursor results=db.query(db_table, new String[]{key_id,longtitude,
latitude},key_id+"="+id, null, null, null, null);
return ConvertToLocations(results);
}
/***
* 更新 单条记录
* @return
*/
public long updateOneLocation(long id,Location location){
ContentValues updateValues=new ContentValues();
updateValues.put(longtitude , location.getLongtitude());
updateValues.put(latitude, location.getLatitude());
return db.update(db_table, updateValues, key_id+"="+id, null);
}
/***
* 返回单个、多个location
* @param cursor
* @return
*/
private Location[] ConvertToLocations(Cursor cursor){
int resultCounts=cursor.getCount();
if(resultCounts==0||!cursor.moveToFirst()){
return null;
}
Location[] locations=new Location[resultCounts];
for(int i=0; i<resultCounts;i++){
locations[i]=new Location();
locations[i].setId(cursor.getInt(0));
locations[i].setLongtitude(cursor.getString(1));
locations[i].setLatitude(cursor.getString(2));
cursor.moveToNext();
}
return locations;
}
}
//location.class
package com.example.sqlitetext;
public class Location {
public Location() {
// TODO Auto-generated constructor stub
}
private int id;
private String latitude;//纬度
private String longtitude;//经度
/***
* 构造函数,初始化字段
* @param id
* @param latitude
* @param longtitude
*/
public Location(String latitude, String longtitude) {
super();
//this.id = id;
this.latitude = latitude;
this.longtitude = longtitude;
}
//属性封装
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongtitude() {
return longtitude;
}
public void setLongtitude(String longtitude) {
this.longtitude = longtitude;
}
/***
* toString()方法
*/
@Override
public String toString() {
return "Location [id=" + id + ", latitude=" + latitude
+ ", longtitude=" + longtitude + "]";
}
}
//MainActivity.class
package com.example.sqlitetext;
import com.example.sqlitetext.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/***
* sqlite操作类,先存字符串
* @author Administrator
*
*/
public class MainActivity extends Activity {
private EditText longtitude,latitude;
private Button add,selete,delect,update;
private Location location,location2[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
longtitude=(EditText)findViewById(R.id.editText1);
latitude=(EditText)findViewById(R.id.editText2);
add=(Button)findViewById(R.id.button1);
selete=(Button)findViewById(R.id.button2);
delect=(Button)findViewById(R.id.button3);
update=(Button)findViewById(R.id.button4);
location2=new Location[10];
//获取不了控件值不了,因为在创建视图时这里已经运行了
//location=new Location(latitude.getText().toString(), longtitude.getText().toString());
System.out.println("latitude.getText().toString()"+latitude.getText().toString());
// Log.i("Main","kk:"+latitude.getText().toString());
//Toast.makeText(this, "latitude.getText().toString():"+latitude.getText().toString(), 1).show();
//添加
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(MainActivity.this, "latitude.getText().toString():"+latitude.getText().toString(), 1).show();
//初始化location
location=new Location(latitude.getText().toString(), longtitude.getText().toString());
DBAdapter dbAdapter=new DBAdapter(MainActivity.this);
dbAdapter.open();//创建数据库
dbAdapter.insert(location);//添加单个记录
dbAdapter.close();//关闭数据库
}
});
//查询
selete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DBAdapter dbAdapter=new DBAdapter(getApplicationContext());
dbAdapter.open();//创建数据库
location2= dbAdapter.queryAllLocations();//查询所有记录
System.out.println("aa: "+location2[0].toString());
//显示在控件上
longtitude.setText(location2[0].getLongtitude());
latitude.setText(location2[0].getLatitude());
}
});
//删除所有
delect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DBAdapter dbAdapter=new DBAdapter(getApplicationContext());
dbAdapter.open();//创建数据库
dbAdapter.deleteAllData();//查询所有记录,返回受影响行数
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<!--main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C0D9D9"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sqlite管理界面"
android:textColor="#38B0DE"
android:textSize="30dip"
android:paddingLeft="55dip"
android:layout_marginTop="71dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="10dip"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:textSize="30dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:hint="longtitude"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:ems="10"
android:inputType="textPersonName"
android:textSize="30dip"
android:hint="latitude"
>
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip"
android:textColor="#38B0DE"
android:layout_marginLeft="201dip"
android:text="添加" />
<!-- 界面要有相同大小才可以表现出来相同的界面 -->
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip"
android:textColor="#38B0DE"
android:text="查询" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip"
android:textColor="#38B0DE"
android:layout_marginLeft="201dip"
android:text="删除" />
<!-- 界面要有相同大小才可以表现出来相同的界面 -->
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip"
android:textColor="#38B0DE"
android:text="更新" />
</LinearLayout>
</LinearLayout>