0

我有一个应用程序将 CurrentHour 存储在 sqlite 数据库中,然后将其显示在另一个活动中。这是我第一次使用 TimePicker - 所以我不确定自己到底做错了什么,但是当我添加到存储和查看 CurrentHour 的成功代码时 - 当我尝试为 CurrentMinute 添加代码(基于 CurrentHour)时应用程序不会保存数据。

add_country.xml (我试图从中存储分钟值的 TimePicker 所在的位置)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" android:layout_weight="1">

<LinearLayout android:id="@+id/linearLayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" 
  android:padding="5dp">

  <EditText android:id="@+id/nameEdit"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" 
     android:imeOptions="actionNext" 
     android:hint="@string/name_hint"
     android:inputType="textPersonName|textCapWords"/>

  <EditText android:id="@+id/capEdit"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:imeOptions="actionNext" 
     android:hint="@string/cap_hint"
     android:inputType="textPersonName|textCapWords"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Data Limit"
    android:textColor="#ffffff"
    android:textAppearance="?android:textAppearanceMedium" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="left"
        android:textColor="#ffffff"
        android:text="10MB" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="right"
        android:textColor="#ffffff"
        android:text="Unlimited Data" />
</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bandwidth Limit"
    android:textColor="#ffffff"
    android:textAppearance="?android:textAppearanceMedium" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:gravity="left"
        android:textColor="#ffffff"
        android:text="10kbs" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:textColor="#ffffff"
        android:gravity="right"
        android:text="Unlimited Bandwidth" />
</LinearLayout>

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceSmall" />

 <TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WiFi Time Limit"
    android:textColor="#ffffff"
    android:textAppearance="?android:textAppearanceMedium" />

<TimePicker
    android:id="@+id/timeEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_weight="1.0" />

 <EditText
    android:id="@+id/codeEdit"
    android:inputType="textUri"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:lines="1"
    android:hint="@string/code_hint"
    android:imeOptions="actionNext" />




  <Button android:id="@+id/saveBtn" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="15dp"
     android:layout_gravity="center_horizontal"
     android:text="@string/save_btn"/>
</LinearLayout>
</ScrollView>

添加国家/地区 JAVA

来自 TimePicker 的很可能的分钟值在这里搞砸了

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;

public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;
 private TimePicker timeEt;
 private TimePicker minEt;

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.add_country);

      nameEt = (EditText) findViewById(R.id.nameEdit);
      capEt = (EditText) findViewById(R.id.capEdit);
      codeEt = (EditText) findViewById(R.id.codeEdit);
      timeEt = (TimePicker) findViewById(R.id.timeEdit);
      minEt = (TimePicker) findViewById(R.id.timeEdit);


      Bundle extras = getIntent().getExtras(); 

      if (extras != null)
      {
         rowID = extras.getLong("row_id");
         nameEt.setText(extras.getString("name"));  
         capEt.setText(extras.getString("cap"));  
         codeEt.setText(extras.getString("code"));  
         timeEt.setCurrentHour(extras.getInt("time"));
         minEt.setCurrentMinute(extras.getInt("min"));
      }

      Button saveButton =(Button) findViewById(R.id.saveBtn);
      saveButton.setOnClickListener(new OnClickListener() {

          public void onClick(View v) 
          {
             if (nameEt.getText().length() != 0)
             {
                AsyncTask<Object, Object, Object> saveContactTask = 
                   new AsyncTask<Object, Object, Object>() 
                   {
                      @Override
                      protected Object doInBackground(Object... params) 
                      {
                         saveContact();
                         return null;
                      }

                      @Override
                      protected void onPostExecute(Object result) 
                      {
                         finish();
                      }
                   }; 

                saveContactTask.execute((Object[]) null); 
             }

             else
             {
                AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
                alert.setTitle(R.string.errorTitle); 
                alert.setMessage(R.string.errorMessage);
                alert.setPositiveButton(R.string.errorButton, null); 
                alert.show();
             }
          } 
     });
   }


       private void saveContact() 
       {
          DatabaseConnector dbConnector = new DatabaseConnector(this);

          if (getIntent().getExtras() == null)
          {
              dbConnector.insertContact(nameEt.getText().toString(),
                      capEt.getText().toString(),
                      timeEt.getCurrentHour().toString(),
                      minEt.getCurrentMinute().toString(),
                      codeEt.getText().toString());
          }
          else
          {
             dbConnector.updateContact(rowID,
                nameEt.getText().toString(),
                capEt.getText().toString(),
                timeEt.getCurrentHour().toString(), /* Storing as String*/
                minEt.getCurrentMinute().toString(), 
                codeEt.getText().toString());
          }
       }
}

数据库连接器 Java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DatabaseConnector {

private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

public DatabaseConnector(Context context) {
    dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}

   public void open() throws SQLException 
   {
      //open database in reading/writing mode
      database = dbOpenHelper.getWritableDatabase();
   } 

   public void close() 
   {
      if (database != null)
         database.close();
   }       

   public void insertContact(String name, String cap, String code, String time, String min) 
           {
              ContentValues newCon = new ContentValues();
              newCon.put("name", name);
              newCon.put("cap", cap);
              newCon.put("time", time);
              newCon.put("min", min);
              newCon.put("code", code);

              open();
              database.insert("country", null, newCon);
              close();
           }


           public void updateContact(long id, String name, String cap,String code,  String time, String min) 
           {
              ContentValues editCon = new ContentValues();
              editCon.put("name", name);
              editCon.put("cap", cap);
              editCon.put("time", time);
              editCon.put("min", min);
              editCon.put("code", code);

              open();
              database.update("country", editCon, "_id=" + id, null);
              close();
           }


           public Cursor getAllContacts() 
           {
              return database.query("country", new String[] {"_id", "name"}, 
                 null, null, null, null, "name");
           }

           public Cursor getOneContact(long id) 
           {
              return database.query("country", null, "_id=" + id, null, null, null, null);
           }

           public void deleteContact(long id) 
           {
              open(); 
              database.delete("country", "_id=" + id, null);
              close();
           }
}

数据库打开助手.JAVA

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

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

@Override
public void onCreate(SQLiteDatabase db) {
    String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, time, min);";                 
    db.execSQL(createQuery);        
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

附言

我希望将 CurrentHour 和 CurrentMinute 同时显示在 view_country.xml 的同一行(如果可能)

<TextView 
android:id="@+id/timeEdit"
style="@style/StyleText"/>  

如果不是 - 只是获得一些帮助传递数据会很棒!(一旦我弄清楚为什么数据没有通过,我会弄清楚其余的)

日志猫:

03-22 08:41:10.120: D/Activity(9149): Activity.onPause(), editTextTapSensorList size: 0
03-22 08:41:10.240: I/Adreno200-EGLSUB(9149): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 08:41:10.240: D/memalloc(9149): ion: Mapped buffer base:0x5d981000 size:614400 offset:0 fd:69
03-22 08:41:10.300: D/memalloc(9149): ion: Mapped buffer base:0x5da29000 size:614400 offset:0 fd:75
03-22 08:41:10.350: D/memalloc(9149): ion: Mapped buffer base:0x5debf000 size:614400 offset:0 fd:78 
03-22 08:41:10.360: D/memalloc(9149): ion: Unmapping buffer  base:0x5d05e000 size:614400
03-22 08:41:10.360: D/memalloc(9149): ion: Unmapping buffer  base:0x5d12e000 size:614400
03-22 08:41:10.360: D/memalloc(9149): ion: Unmapping buffer  base:0x5d385000 size:614400
03-22 08:41:16.316: E/SQLiteDatabase(9149): Error inserting min= time=41 code=8 cap= name=Tttttsstttt
03-22 08:41:16.316: E/SQLiteDatabase(9149): android.database.sqlite.SQLiteException: table country has no column named min: , while compiling: INSERT INTO country(min,time,code,cap,name) VALUES (?,?,?,?,?)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1736)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1609)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at    com.nfc.linkingmanager.DatabaseConnector.insertContact(DatabaseConnector.java:42)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at com.nfc.linkingmanager.AddEditCountry.saveContact(AddEditCountry.java:96)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at  com.nfc.linkingmanager.AddEditCountry.access$1(AddEditCountry.java:90)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at com.nfc.linkingmanager.AddEditCountry$1$1.doInBackground(AddEditCountry.java:63)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-22 08:41:16.316: E/SQLiteDatabase(9149):     at java.lang.Thread.run(Thread.java:856)
03-22 08:41:16.316: D/Activity(9149): Activity.onPause(), editTextTapSensorList size: 0
03-22 08:41:16.336: I/Adreno200-EGLSUB(9149): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 08:41:16.347: D/memalloc(9149): ion: Mapped buffer base:0x5d05e000 size:614400 offset:0 fd:58
03-22 08:41:16.377: D/memalloc(9149): ion: Mapped buffer base:0x5d12e000 size:614400 offset:0 fd:62
03-22 08:41:16.407: D/memalloc(9149): ion: Unmapping buffer  base:0x5d981000 size:614400
03-22 08:41:16.407: D/memalloc(9149): ion: Unmapping buffer  base:0x5da29000 size:614400
03-22 08:41:16.407: D/memalloc(9149): ion: Unmapping buffer  base:0x5debf000 size:614400
03-22 08:41:16.457: D/memalloc(9149): ion: Mapped buffer base:0x5d385000 size:614400 offset:0 fd:66
4

2 回答 2

0

错误很明显。您尝试将值插入到不存在的列中:

table country has no column named min: , while compiling: INSERT INTO country(min,time,code,cap,name) VALUES (?,?,?,?,?)

请注意使用正确的列定义。

    String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, cap, code, time, min);";  <==HERE



 public void insertContact(String name, String cap, String code, String time, String min) 
       {
          ContentValues newCon = new ContentValues();
          newCon.put("name", name);
          newCon.put("cap", cap);
          newCon.put("time", time);
          newCon.put("min", min); <== HERE
          newCon.put("code", code);





       public void updateContact(long id, String name, String cap,String code,  String time, String min) 
       {
          ContentValues editCon = new ContentValues();
          editCon.put("name", name);
          editCon.put("cap", cap);
          editCon.put("time", time);
          editCon.put("min", min); <== HERE
          editCon.put("code", code);

min是一个数据库聚合函数。我认为您不能将其用作列定义。

于 2013-03-22T12:55:35.130 回答
0

异常清楚地表明该country表没有min列。

SQLiteException: table country has no column named min: , while compiling: INSERT INTO country(min,time,code,cap,name) VALUES (?,?,?,?,?)

于 2013-03-22T12:55:48.943 回答