實驗8 SQLite數據庫操做

實驗報告

課程名稱java

基於Android平臺移動互聯網開發android

實驗日期sql

2016年5月6日數據庫

實驗項目名稱數據結構

SQLite數據庫操做app

實驗地點ide

S30010佈局

實驗類型post

□驗證型    √設計型    □綜合型this

學  時

 

1、實驗目的及要求(本實驗所涉及並要求掌握的知識點)

  1. 設計一個我的通信錄,掌握Android平臺下的數據庫開發,該我的通信錄主要包括聯繫人列表和聯繫人詳細信息等界面。
  2. 程序主界面是通信錄的目錄顯示手機上聯繫人的名稱。點擊聯繫人的姓名能夠顯示聯繫人的詳細信息。單擊圖標按鈕能夠添加聯繫人和刪除聯繫人。

 

2、實驗環境(本實驗所使用的硬件設備和相關軟件)

(1)PC機

(2)操做系統:Windows XP

(3)軟件: Eclipse, JDK1.6,Android SDK,ADT

3、實驗內容及步驟

(1)  肯定數據庫的數據結構。

(2)  新建工程,修改佈局文件,定義字符串資源。

(3)  開發佈局文件activity_main.xml用於顯示聯繫人列表。

(4)  layout目錄下新建一個detail.xml,用於顯示聯繫人詳細信息。

(5)  開發數據庫輔助類MyOpenHelper類

(6)  DetailActivity端開發實現數據庫增長、刪除、修改記錄等操做

(7)  新建Activity名爲DetailActivity.java,實現聯繫人詳細信息顯示功能。

 

4、實驗結果(本實驗源程序清單及運行結果或實驗結論、實驗設計圖)

代碼:

detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tname"
            android:layout_width="77dp"
            android:layout_height="wrap_content"
            android:text="   姓名:   "
            android:textSize="18dp"
            />

        <EditText
            android:id="@+id/ename"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPersonName" >

            <requestFocus />
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tguhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="固定電話:"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/eguhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="phone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tshouji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.00"
            android:text="移動電話:" 
            android:textSize="18dp"/>

        <EditText
            android:id="@+id/eshouji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.00"
            android:ems="10"
            android:inputType="phone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/temail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="電子郵件:" 
            android:textSize="18dp"/>

        <EditText
            android:id="@+id/eemail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textEmailAddress" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tpost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="郵政編碼:"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/epost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tadress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通信地址:" 
            android:textSize="18dp"/>

        <EditText
            android:id="@+id/eadress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPostalAddress" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tgongsi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="公司名稱:" 
            android:textSize="18dp"/>

        <EditText
            android:id="@+id/egongsi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

    </LinearLayout>

    <Button
        android:id="@+id/btnsave"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存" />

</LinearLayout>

Myopenhelper.java

package com.example.contacts;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Myopenhelper extends SQLiteOpenHelper{
    
    public static final String DB_NAME="personal_contacts"; //數據庫文件名
    public static final String TABLE_NAME="contacts";        //表名
    public static final String ID="id";                      //ID
    public static final String NAME="name";                  //名稱
    public static final String PHONE="phone";                //固話
    public static final String MOBTLE="mobile";               //手機
    public static final String EMAIL="email";                 //email
    public static final String POST="post";                   //郵編
    public static final String ADD="address";               //地址
    public static final String GONGSI="gongsi";             //公司

    public Myopenhelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("creat table id no exists"+TABLE_NAME+"("
                +ID+"integer primary key,"
                +NAME+"varchar,"
                +PHONE+"varchar,"
                +MOBTLE+"varchar,"
                +EMAIL+"varchar,"
                +POST+"varchar,"
                +ADD+"varchar,"
                +GONGSI+"varchar)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("if exists"+TABLE_NAME);
        onCreate(db);
        Log.e("Datsbase", "onUpgrade");
    }

}

DetailActivity.java

package com.example.contacts;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
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.EditText;
import android.widget.Toast;

public class DetailActivity extends Activity {
    EditText ename, eguhua, eshouji, eemail, epost, eadress, egongsi;
    String name, guhua, shouji, email, post, adress, gongsi;
    Button btnsave;
    
    SQLiteDatabase userdatabase;
    Bundle bd;
    String selectname;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
        btnsave = (Button) findViewById(R.id.btnsave);
        ename = (EditText) findViewById(R.id.ename);
        eguhua = (EditText) findViewById(R.id.eguhua);
        eshouji = (EditText) findViewById(R.id.eshouji);
        eemail = (EditText) findViewById(R.id.eemail);
        epost = (EditText) findViewById(R.id.epost);
        eadress = (EditText) findViewById(R.id.eadress);
        egongsi = (EditText) findViewById(R.id.egongsi);
      
        bd = getIntent().getExtras();
        if (!bd.getString("name").equals("")) {
            selectname = bd.getString("name");
            Cursor cursor = userdatabase.rawQuery(
                    "select * from userTable where name=?",
                    new String[] { selectname });
            cursor.moveToFirst();
            ename.setText(cursor.getString(cursor.getColumnIndex("name")));
            eguhua.setText(cursor.getString(cursor.getColumnIndex("guhua")));
            eshouji.setText(cursor.getString(cursor.getColumnIndex("shouji")));
            eemail.setText(cursor.getString(cursor.getColumnIndex("email")));
            epost.setText(cursor.getString(cursor.getColumnIndex("post")));
            eadress.setText(cursor.getString(cursor.getColumnIndex("adress")));
            egongsi.setText(cursor.getString(cursor.getColumnIndex("gongsi")));
            btnsave.setText("保存");
        }
        btnsave.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                name = ename.getText().toString();
                guhua = eguhua.getText().toString();
                shouji = eshouji.getText().toString();
                email = eemail.getText().toString();
                post = epost.getText().toString();
                adress = eadress.getText().toString();
                gongsi = egongsi.getText().toString();

                if (!name.equals("") && !guhua.equals("") && !shouji.equals("")
                        && !email.equals("") && !post.equals("")
                        && !adress.equals("") && !gongsi.equals("")) {

                    Cursor cursor = userdatabase.rawQuery(
                            "select * from userTable where name=?",
                            new String[] { name });
                    cursor.moveToFirst();
                    ContentValues cv = new ContentValues();
                    cv.put("name", name);
                    cv.put("phone", guhua);
                    cv.put("mobile", shouji);
                    cv.put("email", email);
                    cv.put("post", post);
                    cv.put("addr", adress);
                    cv.put("comp", gongsi);
                    if (cursor.getCount() <= 0) {
                        userdatabase.insert("userTable", null, cv);
                        cv.clear();
                        Toast.makeText(DetailActivity.this, "保存" + name + "成功",
                                Toast.LENGTH_LONG).show();
                        userdatabase.delete("userTable", "name=?",
                                new String[] { selectname });

                        Intent it = new Intent();
                        it.setClass(DetailActivity.this, MainActivity.class);
                        startActivity(it);
                        finish();
                    } else if (cursor.getCount() == 1
                            && cursor.getString(cursor.getColumnIndex("name"))
                                    .equals(selectname)) {
                        userdatabase.update("userTable", cv, "name=?",
                                new String[] { selectname });
                        cv.clear();
                        Toast.makeText(DetailActivity.this, "更新" + name + "成功",
                                Toast.LENGTH_LONG).show();
                        Intent it = new Intent();
                        it.setClass(DetailActivity.this, MainActivity.class);
                        startActivity(it);
                        finish();
                    } else {

                        Toast.makeText(DetailActivity.this, name + "已註冊",
                                Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(DetailActivity.this, "信息不完整",
                            Toast.LENGTH_LONG).show();
                }
            }

        });
    }
}

MainActivity.java

package com.example.contacts;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    
    private ListView lv;
    private Button addbtn;
    private Button deletebtn;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
              
                 addbtn=(Button)findViewById(R.id.button1);

                 addbtn.setOnClickListener(new OnClickListener() {

                     public void onClick(View arg0) {

                         Intent intent2=new Intent();

                         intent2.setClass(MainActivity.this, DetailActivity.class);

                         startActivity(intent2);

                         finish();  

                     }

                 });
                 
                 deletebtn=(Button) findViewById(R.id.button2);
                 
                 deletebtn.setOnClickListener(new OnClickListener() {
                    
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
//                        db.delete("contacts","name=?",new string[]{s});
                    }
                });

             }

        }
        
    


    @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;
    }
    
}

 

5、實驗總結(對本實驗結果進行分析,實驗心得體會及改進意見)

 此次的實驗沒有成功。

相關文章
相關標籤/搜索