初次提交
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample2_4</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,12 @@
|
||||
#Fri Jul 01 08:03:18 CST 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.bn.pp4"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<application android:icon="@drawable/icon" android:label="@string/app_name">
|
||||
<activity android:name=".Sample2_4_Activity"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<provider
|
||||
android:name="MyContentProvider"
|
||||
android:authorities="com.bn.pp4.provider.student"
|
||||
/>
|
||||
</application>
|
||||
|
||||
|
||||
</manifest>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,11 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Project target.
|
||||
target=android-7
|
||||
@@ -0,0 +1,31 @@
|
||||
/* AUTO-GENERATED FILE. DO NOT MODIFY.
|
||||
*
|
||||
* This class was automatically generated by the
|
||||
* aapt tool from the resource data it found. It
|
||||
* should not be modified by hand.
|
||||
*/
|
||||
|
||||
package com.bn.pp4;
|
||||
|
||||
public final class R {
|
||||
public static final class attr {
|
||||
}
|
||||
public static final class drawable {
|
||||
public static final int icon=0x7f020000;
|
||||
}
|
||||
public static final class id {
|
||||
public static final int Button01=0x7f050001;
|
||||
public static final int Button02=0x7f050002;
|
||||
public static final int Button03=0x7f050003;
|
||||
public static final int Button04=0x7f050004;
|
||||
public static final int Button05=0x7f050005;
|
||||
public static final int TextView01=0x7f050000;
|
||||
}
|
||||
public static final class layout {
|
||||
public static final int main=0x7f030000;
|
||||
}
|
||||
public static final class string {
|
||||
public static final int app_name=0x7f040001;
|
||||
public static final int hello=0x7f040000;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
>
|
||||
<TextView
|
||||
android:text="SQLite的基础操作"
|
||||
android:id="@+id/TextView01"
|
||||
android:gravity="center"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</TextView>
|
||||
<Button
|
||||
android:text="创建/打开数据库"
|
||||
android:id="@+id/Button01"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
<Button
|
||||
android:text="关闭数据库"
|
||||
android:id="@+id/Button02"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
<Button
|
||||
android:text="添加记录"
|
||||
android:id="@+id/Button03"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
<Button
|
||||
android:text="删除记录"
|
||||
android:id="@+id/Button04"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
<Button
|
||||
android:text="查询记录"
|
||||
android:id="@+id/Button05"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content">
|
||||
</Button>
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="hello">Hello World, Sample3_4_Activity!</string>
|
||||
<string name="app_name">Sample2_4</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.bn.pp4;
|
||||
import android.content.ContentProvider; //导入相关包
|
||||
import android.content.ContentValues;
|
||||
import android.content.UriMatcher;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.net.Uri;
|
||||
public class MyContentProvider extends ContentProvider { // 继承ContentProvider
|
||||
private static final UriMatcher um; // 声明Uri匹配引用
|
||||
static {
|
||||
um = new UriMatcher(UriMatcher.NO_MATCH); // 创建UriMatcher
|
||||
um.addURI("com.bn.pp4.provider.student", "stu", 1); // 设置匹配字符串
|
||||
}
|
||||
SQLiteDatabase sld; // 声明SQLiteDatabase引用
|
||||
@Override
|
||||
public String getType(Uri uri) {
|
||||
return null;
|
||||
}
|
||||
@Override // 调用数据库的query方法时会自动调用该方法
|
||||
public Cursor query(Uri uri, String[] projection, String selection,
|
||||
String[] selectionArgs, String sortOrder) {
|
||||
switch (um.match(uri)) {// 若匹配成功
|
||||
case 1: // 执行操作,获取Cursor对象引用
|
||||
Cursor cur = sld.query("student", projection, selection,
|
||||
selectionArgs, null, null, sortOrder);
|
||||
return cur; // 返回Cursor对象引用
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public int delete(Uri arg0, String arg1, String[] arg2) {// 空实现
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri insert(Uri uri, ContentValues values) {// 空实现
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public boolean onCreate() { // 创建数据库时自动调用该方法
|
||||
sld = SQLiteDatabase.openDatabase(
|
||||
"/data/data/com.bn.pp4/mydb", // 数据库所在路径
|
||||
null, // 游标工厂,默认为null
|
||||
SQLiteDatabase.OPEN_READWRITE|
|
||||
SQLiteDatabase.CREATE_IF_NECESSARY // 读写、若不存在则创建
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public int update(Uri uri, ContentValues values, String selection,
|
||||
String[] selectionArgs) {// 空实现
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.bn.pp4;
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
public class Sample2_4_Activity extends Activity {
|
||||
SQLiteDatabase sld; // 声明SQLiteDatabase引用
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) { // 重新onCreate方法
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.main); // 设置layout
|
||||
Button b = (Button) this.findViewById(R.id.Button01); // 获取打开/创建数据库按钮
|
||||
b.setOnClickListener( // 为打开/创建按钮添加监听器
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
createOrOpenDatabase(); // 调用方法打开或创建数据库
|
||||
}
|
||||
});
|
||||
b = (Button) this.findViewById(R.id.Button02);// 获取关闭数据库按钮
|
||||
b.setOnClickListener( // 为关闭按钮添加监听器
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
closeDatabase(); // 调用方法关闭数据库
|
||||
}
|
||||
});
|
||||
b = (Button) this.findViewById(R.id.Button03); // 获取添加记录按钮
|
||||
b.setOnClickListener( // 为添加按钮添加监听器
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
insert(); // 调用方法插入记录
|
||||
}
|
||||
});
|
||||
b = (Button) this.findViewById(R.id.Button04);// 获取删除记录按钮
|
||||
b.setOnClickListener( // 为删除按钮添加监听器
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
delete(); // 调用方法删除记录
|
||||
}
|
||||
});
|
||||
b = (Button) this.findViewById(R.id.Button05); // 获取查询记录按钮
|
||||
b.setOnClickListener( // 为查询按钮添加监听器
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
query(); // 调用方法查询记录
|
||||
}
|
||||
});
|
||||
}
|
||||
public void createOrOpenDatabase() {// 创建或打开数据库的方法
|
||||
try {
|
||||
sld = SQLiteDatabase.openDatabase(
|
||||
"/data/data/com.bn.pp4/mydb", // 数据库所在路径
|
||||
null, // 游标工厂,默认为null
|
||||
SQLiteDatabase.OPEN_READWRITE |
|
||||
SQLiteDatabase.CREATE_IF_NECESSARY // 模式为读写,若不存在则创建
|
||||
);
|
||||
// 生成创建数据库的sql语句
|
||||
String sql = "create table if not exists student" +
|
||||
"(sno char(5),stuname varchar(20)," +
|
||||
"sage integer,sclass char(5))";
|
||||
sld.execSQL(sql); // 执行sql语句
|
||||
Toast.makeText(getBaseContext(), "成功创建数据库。",
|
||||
Toast.LENGTH_LONG).show();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void closeDatabase() {// 关闭数据库的方法
|
||||
try {
|
||||
sld.close(); // 关闭数据库
|
||||
Toast.makeText(getBaseContext(), "成功关闭数据库。",
|
||||
Toast.LENGTH_LONG).show();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void insert() {// 插入记录的方法
|
||||
try {// 生成插入记录的sql语句
|
||||
String sql = "insert into student values" +
|
||||
"('001','Android',22,'283')";
|
||||
sld.execSQL(sql); // 执行sql语句
|
||||
Toast.makeText(getBaseContext(), "成功插入一条记录。",
|
||||
Toast.LENGTH_LONG).show();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void delete() {// 删除记录的方法
|
||||
try {// 生成删除所有记录的sql语句
|
||||
String sql = "delete from student;";
|
||||
sld.execSQL(sql); // 执行sql语句
|
||||
Toast.makeText(getBaseContext(), "成功删除所有记录。",
|
||||
Toast.LENGTH_LONG).show();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void query(){// 查询的方法
|
||||
try {// 生成查询记录的sql语句
|
||||
String sql = "select * from student where sage>?";
|
||||
Cursor cur = sld.rawQuery(sql, new String[] { "20" }); // 获取Cursor对象引用
|
||||
while (cur.moveToNext()) {// 若存在记录
|
||||
String sno = cur.getString(0); // 获取第一列信息
|
||||
String sname = cur.getString(1); // 获取第二列信息
|
||||
int sage = cur.getInt(2); // 获取第三列信息
|
||||
String sclass = cur.getString(3); // 获取第四列信息
|
||||
Toast.makeText(
|
||||
getBaseContext(),
|
||||
"查询到的记录为:'" + sno + "'\t'" + sname
|
||||
+ "'\t\t'" + sage+ "'\t'" + sclass + "'",
|
||||
Toast.LENGTH_LONG).show();
|
||||
}
|
||||
cur.close(); // 关闭Cursor
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}}}
|
||||
Reference in New Issue
Block a user