安卓工作室项目

Posted

技术标签:

【中文标题】安卓工作室项目【英文标题】:Android Studio project 【发布时间】:2017-11-30 15:02:03 【问题描述】:

您好,我正在做一个 andorid 工作室项目,当我将数据添加到应用程序然后尝试查看数据时出现错误,提示“错误,未找到任何内容”。我自己做了一个 SQLite 数据库,希望有人能帮我找出错误。

package ie.wit.andrew.drivingschool;

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

public class DatabaseHelper extends SQLiteOpenHelper 
public static final String DATABASE_NAME = "Driver.db";
public static final String TABLE_NAME = "driver_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "DATE OF BIRTH";
public static final String COL_4 = "LOGBOOK NUMBER";   //Making up my 
                                                       //database of the 
                                                       //information I will 
                                                       //be entering into my 
                                                       //application
public static final String COL_5 = "LESSON NUMBER";

public DatabaseHelper(Context context) 
    super(context, DATABASE_NAME, null, 1); //when this constructor is 
                                            //called your Database has been 
                                            //created



@Override
public void onCreate(SQLiteDatabase db) 
    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY 
AUTOINCREMENT,NAME TEXT,DATE OF BIRTH TEXT, LOGBOOK NUMBER TEXT, LESSON 
NUMBER INTEGER)");


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);



public boolean insertData(String name, String dateofbirth, String 
logbooknumber, String lessonnumber) 
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,dateofbirth);
    contentValues.put(COL_4,logbooknumber);
    contentValues.put(COL_5,lessonnumber);
    long result = db.insert(TABLE_NAME,null,contentValues); //This method 
                                                            //returns -1
    if (result == -1)
        return false;
    else
        return true;


public Cursor getAllData() 
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+ TABLE_NAME,null);
    return res;





package ie.wit.andrew.drivingschool;

import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DrivingSchool extends AppCompatActivity 

DatabaseHelper myDb;
EditText editName,editDateofBirth,editLogbookNumber,editLessonNumber;
Button btnAddData;
Button btnviewAll;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driving_school);
    myDb = new DatabaseHelper(this);

    editName = (EditText)findViewById(R.id.editText_name);
    editDateofBirth = (EditText)findViewById(R.id.editText_dateofbirth);
    editLogbookNumber = (EditText)findViewById(R.id.editText_logbooknumber);
    editLessonNumber = (EditText)findViewById(R.id.editText_lessonNumber);
    btnAddData = (Button)findViewById(R.id.button_add);
    btnviewAll = (Button)findViewById(R.id.button_viewAll);
    AddData();
    viewAll();
 

   public void AddData() 
    btnAddData.setOnClickListener(
            new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                  boolean isInserted = 
    myDb.insertData(editName.getText().toString(),
                            editDateofBirth.getText().toString(),
                            editLogbookNumber.getText().toString(),
                            editLessonNumber.getText().toString());
                    if(isInserted = true)
                        Toast.makeText(DrivingSchool.this,"Data 
     Inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(DrivingSchool.this,"Data not 
   Inserted", Toast.LENGTH_LONG).show();
                 
              
      );
   

  public  void viewAll() 
    btnviewAll.setOnClickListener(
            new View.OnClickListener() 
                @Override
                public void onClick(View v)
                    Cursor res = myDb.getAllData();
                     if(res.getCount() == 0) 
                          //Show Message
                         showMessage("Error", "Nothing found");
                         return;
                     

                     StringBuffer buffer = new StringBuffer();
                    while(res.moveToNext()) 
                        buffer.append("ID : "+ res.getString(0)+"\n");
                        buffer.append("NAME : "+ res.getString(1)+"\n");
                        buffer.append("DATE OF BIRTH : "+ 
                        res.getString(2)+"\n");
                        buffer.append("LOGBOOK NUMBER : "+ 
                        res.getString(3)+"\n\n");;

                    

                    //showdata
                    showMessage("Data",buffer.toString());
                
            
      );

  

  public void showMessage(String title,String Message)
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(title);
        builder.setMessage(Message);
        builder.show();
  

【问题讨论】:

在调用 sqlite 的地方添加代码 onCreate(SQLiteDatabase db) 中的错误(删除 DATE OF BIRTH 和其他列之间的空格) 你能给我们确切的错误信息和/或堆栈跟踪吗? 我点击添加数据,然后点击查看数据,我得到的错误信息是“ERROR NOTHING FOUND” 【参考方案1】:

尝试从列名中删除空格

【讨论】:

我试过删除空格,不幸的是我得到了同样的错误【参考方案2】:

您提供了错误的列名,这就是您的数据库未创建的原因,请查看此链接以获取在 Android 应用程序中创建和使用数据库的完整指南

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

【讨论】:

以上是关于安卓工作室项目的主要内容,如果未能解决你的问题,请参考以下文章

修改 安卓工作室 启动页

您的项目位置包含空格。 (安卓工作室)

多级列表视图从一个项目中打开两个活动。安卓工作室 |爪哇

安卓工作室 3.6.1 |错误:“此项目使用 AndroidX 依赖项”[重复]

序言中不能有内容。在安卓工作室

任务 ':app:compileDebugJavaWithJavac' 执行失败。安卓工作室 [关闭]